110. Balanced Binary Tree
# Easy, DFS
Solution 1:
# 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: TreeNode) -> bool:
# edge case
if root == None:
return True
# regular case
if abs(self.maxHeight(root.left) - self.maxHeight(root.right)) > 1:
return False
return self.isBalanced(root.left) and self.isBalanced(root.right)
def maxHeight(self, root):
if root == None:
return 0
return max(self.maxHeight(root.left), self.maxHeight(root.right))+1Solution 2:
Last updated