Skip to content

Latest commit

 

History

History
51 lines (36 loc) · 1.25 KB

_110. Balanced Binary Tree.md

File metadata and controls

51 lines (36 loc) · 1.25 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : July 04, 2024

Last updated : July 04, 2024


Related Topics : Tree, Depth-First Search, Binary Tree

Acceptance Rate : 54.1 %


Solutions

Python

# 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:
        def dfs(curr: Optional[TreeNode]) -> int :
            if not curr :
                return 0
            
            left, right = dfs(curr.left), dfs(curr.right)

            if left == -1 or right == -1 :
                return -1

            if abs(left - right) > 1 :
                return -1

            return max(left, right) + 1

        return True if dfs(root) != -1 else False