Only a little different with the accepted answer, but wrong. Why??


  • 0
    L
            if root==None:
                return 0
            elif self.maxDepth(root.left)>self.maxDepth(root.right):
                return self.maxDepth(root.left)+1
            else:
                return self.maxDepth(root.right)+1

  • 0
    L

    Time Limit Exceeded?
    You have called maxDepth too many times.

    For example,

        elif self.maxDepth(root.left)>self.maxDepth(root.right):
                return self.maxDepth(root.left)+1
    

    here,when condition is true(self.maxDepth(root.left) and self.maxDepth(root.right) have been executed), self.maxDepth(root.left) will execute another time before return.

    The following is code(AC) corrected from yours

        if root==None:
            return 0
        DepLeft = self.maxDepth(root.left)
        DepRight = self.maxDepth(root.right)
        if DepLeft>DepRight:
            return DepLeft+1
        else:
            return DepRight+1

Log in to reply
 

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