1, this problem will time out if it is purely recursive and does not use it as a complete binary tree.
2. In order to use this condition, compare the height of the left and right two sub-numbers: 1, if the left subtree is a complete binary tree 2, if not equal, then it is a complete binary tree.
3, the total binary tree node number is POW (2,depth)-1, so you can save time without recursion points.
4, because it is the complete binary tree, the depth of the subtree can only be obtained by searching the left son.
Overall complexity O (LGN * lgn)
classSolution:#@param {TreeNode} root #@return {integer} defCountnodes (self, root):if notRoot:return0 leftdepth=self.getdepth (root.left) rightdepth=self.getdepth (root.right)ifLeftdepth = =rightdepth:returnPow (2, leftdepth) +self.countnodes (root.right)Else: returnPow (2, rightdepth) +self.countnodes (root.left)defgetdepth (self, root):if notRoot:return0return1 + self.getdepth (root.left)
222 Count Complete Tree Nodes