Originally from Unit 9 Session 1 (Click for link to problem statements)
Reviewed in Unit 10 Session 2
Understand what the interviewer is asking for by using test cases and questions about the problem.
- Established a set (2-3) of test cases to verify their own solution later.
- Established a set (1-2) of edge cases to verify their solution handles complexities.
- Have fully understood the problem and have no clarifying questions.
- Have you verified any Time/Space Constraints for this problem?
Can the tree be empty?
What should be returned for a single-node tree?
HAPPY CASE 1
3 (root)
/ \
9 20
/ \
15 7
Input: root
Output: True
Explanation: The tree is balanced as the height difference between the left and right subtrees of every node is not more than 1.
HAPPY CASE 2
1 (root)
/ \
2 2
/ \
3 3
/ \
4 4
Input: root
Output: False
Explanation: The tree is not balanced as the height difference between the left and right subtrees of the node with value 1 is more than 1.
Match what this problem looks like to known categories of problems, e.g. Linked List or Dynamic Programming, and strategies or patterns in those categories.
For Tree problems, we want to consider the following approaches:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Use a recursive approach to check the balance of the tree. For each node, recursively check if the left and right subtrees are balanced and if the height difference is not more than 1.
1) Define a helper function `check_balance_and_height(node)` to check balance and calculate height.
a) If the current node is None, return True (balanced) and height 0.
b) Recursively check the left subtree for balance and height.
c) Recursively check the right subtree for balance and height.
d) Check if the current node is balanced by comparing the heights of the left and right subtrees.
e) Calculate the height of the current node.
f) Return if the current node is balanced and its height.
2) In the main function `is_balanced(root)`, call the helper function to start the check from the root node.
3) Return if the tree is balanced.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def check_balance_and_height(node):
if not node:
return True, 0
left_balanced, left_height = check_balance_and_height(node.left)
right_balanced, right_height = check_balance_and_height(node.right)
balanced = left_balanced and right_balanced and abs(left_height - right_height) <= 1
height = max(left_height, right_height) + 1
return balanced, height
def is_balanced(root):
balanced, height = check_balance_and_height(root)
return balanced
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of nodes in the tree.
O(N)
because we need to traverse all the nodes in the tree.O(N)
for the recursion stack in the worst case when the tree is completely unbalanced.