98% python --- two methods


  • 4
    W
    enter code hereclass Solution(object):
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root ==None:
            return 0
        
        
        stk = [root]
        depth = 0
        while stk:
            nextLevel = []
            depth+=1
            for item in stk:
                if item.left:
                    nextLevel.append(item.left)
                if item.right:
                    nextLevel.append(item.right)
            stk = nextLevel
        return depth
    enter code here   class Solution2(object):
    result = float('-inf')
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root ==None:
            return 0
        self.helper(root,1)
        return self.result
    def helper(self,root,depth):
        if root.left==None and root.right ==None:
            self.result = max(self.result,depth)
        if root.left:
            self.helper(root.left,depth+1)
        if root.right:
            self.helper(root.right,depth+1)

Log in to reply
 

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