-
Notifications
You must be signed in to change notification settings - Fork 0
LC 0110 [E] Balanced Binary Tree
Code with Senpai edited this page Feb 3, 2022
·
1 revision
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
self.balanced = True
def postorder(node):
if not node: return 0
L = postorder(node.left)
R = postorder(node.right)
# ^ depths of left and right subtrees
if abs(L - R) > 1:
self.balanced = False
return max(L, R) + 1
postorder(root)
return self.balanced
footer