Horray finally got a python solution


  • 0
    H
        # Definition for a binary tree node.
        # class TreeNode:
        #     def __init__(self, x):
        #         self.val = x
        #         self.left = None
        #         self.right = None
        
       
    class Solution:
            # @param {TreeNode} root
            # @return {integer}
            
            def _countnodes(self,root,h):
                if root.right==None:
                    return h+1
                curr=root.right
                depth=1
                while curr.left!=None:
                    curr=curr.left
                    depth+=1
                if depth==h:
                    return 2**h+self._countnodes(root.right,h-1)
                return 2**(h-1)+self._countnodes(root.left,h-1)
            def countNodes(self, root):
                if root==None:
                    return 0
                h=0
                curr=root
                while curr.left!=None:
                    curr=curr.left
                    h+=1
                return self._countnodes(root,h)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.