Why does not this intuitive Python solution work?


  • 0
    P
    # 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
            """
            def isBal(rt):
                if not rt:
                    return True
                elif rt.left == None and rt.right == None:
                    return True
                elif rt.left == None:
                    if rt.right.right !=None or rt.right.left != None:
                        return False
                elif rt.right == None:
                    if rt.left.right !=None or rt.left.left != None:
                        return False
                else:
                    return isBal(rt.left) and isBal(rt.right)
            return isBal(root)

Log in to reply
 

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