balanced_binary_tree

 1from utils import TreeNode
 2from typing import Optional
 3
 4
 5# @leet start
 6class Solution:
 7    def isBalanced(self, root: Optional[TreeNode]) -> bool:
 8        """
 9        This code checks is a binary tree is balanced.
10        The balance property of a binary search tree can be said as follows:
11        The depth of the left and right hand side of the tree do not vary by
12        more than 1.
13        Imagine a tree with a left side depth of 1 and right side of 3.
14        This would not be balanced.
15
16        To codify those rules, there's a global variable called `is_balanced`
17        that verifies that the tree is balanced.
18        For each node in the tree, its left and right depths are compared.
19        If there is a difference of more than one, we know the tree is not balanced.
20        Finally, each node returns the maximum of its left and right depth to continue
21        the recursion.
22        """
23        is_balanced = True
24
25        def depth(node, curr_depth):
26            nonlocal is_balanced
27            if not node:
28                return curr_depth
29            left_depth = depth(node.left, curr_depth + 1)
30            right_depth = depth(node.right, curr_depth + 1)
31            if abs(right_depth - left_depth) > 1:
32                is_balanced = False
33            return max(left_depth, right_depth)
34
35        depth(root, 0)
36        return is_balanced
37
38
39# @leet end
40
41
42def test():
43    assert 2 + 2 == 4
class Solution:
 7class Solution:
 8    def isBalanced(self, root: Optional[TreeNode]) -> bool:
 9        """
10        This code checks is a binary tree is balanced.
11        The balance property of a binary search tree can be said as follows:
12        The depth of the left and right hand side of the tree do not vary by
13        more than 1.
14        Imagine a tree with a left side depth of 1 and right side of 3.
15        This would not be balanced.
16
17        To codify those rules, there's a global variable called `is_balanced`
18        that verifies that the tree is balanced.
19        For each node in the tree, its left and right depths are compared.
20        If there is a difference of more than one, we know the tree is not balanced.
21        Finally, each node returns the maximum of its left and right depth to continue
22        the recursion.
23        """
24        is_balanced = True
25
26        def depth(node, curr_depth):
27            nonlocal is_balanced
28            if not node:
29                return curr_depth
30            left_depth = depth(node.left, curr_depth + 1)
31            right_depth = depth(node.right, curr_depth + 1)
32            if abs(right_depth - left_depth) > 1:
33                is_balanced = False
34            return max(left_depth, right_depth)
35
36        depth(root, 0)
37        return is_balanced
def isBalanced(self, root: Optional[utils.TreeNode]) -> bool:
 8    def isBalanced(self, root: Optional[TreeNode]) -> bool:
 9        """
10        This code checks is a binary tree is balanced.
11        The balance property of a binary search tree can be said as follows:
12        The depth of the left and right hand side of the tree do not vary by
13        more than 1.
14        Imagine a tree with a left side depth of 1 and right side of 3.
15        This would not be balanced.
16
17        To codify those rules, there's a global variable called `is_balanced`
18        that verifies that the tree is balanced.
19        For each node in the tree, its left and right depths are compared.
20        If there is a difference of more than one, we know the tree is not balanced.
21        Finally, each node returns the maximum of its left and right depth to continue
22        the recursion.
23        """
24        is_balanced = True
25
26        def depth(node, curr_depth):
27            nonlocal is_balanced
28            if not node:
29                return curr_depth
30            left_depth = depth(node.left, curr_depth + 1)
31            right_depth = depth(node.right, curr_depth + 1)
32            if abs(right_depth - left_depth) > 1:
33                is_balanced = False
34            return max(left_depth, right_depth)
35
36        depth(root, 0)
37        return is_balanced

This code checks is a binary tree is balanced. The balance property of a binary search tree can be said as follows: The depth of the left and right hand side of the tree do not vary by more than 1. Imagine a tree with a left side depth of 1 and right side of 3. This would not be balanced.

To codify those rules, there's a global variable called is_balanced that verifies that the tree is balanced. For each node in the tree, its left and right depths are compared. If there is a difference of more than one, we know the tree is not balanced. Finally, each node returns the maximum of its left and right depth to continue the recursion.

def test():
43def test():
44    assert 2 + 2 == 4