2015-10-14 81 views

回答

1

你可以在下面的僞代碼轉換爲任何語言,按您的喜好。

function count_internal_nodes(curr): 
     if curr == null: return 0 
     else if curr is leaf: return 0 
     else: return 1 + count_internal_node(curr.left) + 
         count_internal_nodes(curr.right) 
0

你可以試試這個算法中

getInteriorNodes(self) 
    count = 0 
    hasLeft, hasRight = self.left<>null, self.right <>null 
    if (hasLeft) 
     count += self.left.getInteriorNodes() 
    else if (hasRight) 
     count += self.right.getInteriorNodes() 
    else if ((hasLeft || hasRight) && self.parent) 
     count += 1 
return count 
+0

你能轉換c?中的代碼嗎? –