Why TLE on this Python code?


  • 0
    F
    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def isBalanced(self, root):
            """
            :type root: TreeNode
            :rtype: bool
            """
            if not root: return True
            return self.getHeightDFS(root)!=-1
        def getHeightDFS(self, root):
            if not root: return 0
            leftH=self.getHeightDFS(root.left)
            if leftH==-1: return -1
            rightH=self.getHeightDFS(root.right)
            if rightH==-1: return -1
            if abs(leftH-rightH)>1: return -1
            return max(self.getHeightDFS(root.left), self.getHeightDFS(root.right))+1

Log in to reply
 

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