Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
998 B
39
Indexable
Never
# 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 height(node):
            if node is None:
                return 0
            
            left=height(node.left)
            right=height(node.right)
            
            return 1+max(left,right)
        
        def check(node):
            
            left=height(node.left)
            right=height(node.right)
            
            if(abs(left-right)>1):
                return False
            
            return True
        
        def traverse(node):
            
            if not node:
                return
            
            if(not check(node)):
                return False
            
            traverse(node.left)
            traverse(node.right)
        
        traverse(root)