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?
O(n)
time complexity and O(1)
excluding the recursion call stack.HAPPY CASE
Input: root = [3,9,20,null,null,15,7]
Output: true
Input: root = [1,2,2,3,3,null,null,4,4]
Output: false
EDGE CASE
Input: root = []
Output: true
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.
If you are dealing with Binary Trees some common techniques you can employ to help you solve the problem:
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Recursively ask for child height in tree and add one for parent. Check the balance at each node by looking at the right and left height of tree.
1. Create function to return the height of tree and collect balance at each node
a. Basecase is a Null Node, return 0
b. Collect information regarding balance of node
c. Recursively return the max between height of left node and right node and add one for current node.
2. Create variable to hold balance information
3. Call the function to check balance of each node
4. Return the balance
β οΈ Common Mistakes
Implement the code to solve the algorithm.
# 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:
# Create function to return the height of tree and collect balance at each node
def helper(root: Optional[TreeNode]) -> int:
# Basecase is a Null Node, return 0
if not root:
return 0
# Collect information regarding balance of node
leftHeight = helper(root.left)
rightHeight = helper(root.right)
if abs(rightHeight - leftHeight) > 1:
nonlocal balanced
balanced = False
return 0
# Recursively return the max between height of left node and right node and add one for current node.
return max(leftHeight, rightHeight) + 1
# Create variable to hold balance information
balanced = True
# Call the function to check balance of each node
helper(root)
# Return the balance
return balanced
class Solution {
public boolean isBalanced(TreeNode root) {
// If the tree is empty, we can say itβs balanced...
if (root == null) return true;
// Call the function to check balance of each node and return the balance
if (Height(root) == -1) return false;
return true;
}
// Create function to return the height of tree and collect balance at each node
public int Height(TreeNode root) {
// Basecase is a Null Node, return 0
if (root == null) return 0;
// Collect information regarding balance of node
int leftHeight = Height(root.left);
int rightHight = Height(root.right);
// Recursively return the max between height of left node and right node and add one for current node.
// In case of left subtree or right subtree unbalanced, return -1...
if (leftHeight == -1 || rightHight == -1) return -1;
// If their heights differ by more than β1β, return -1...
if (Math.abs(leftHeight - rightHight) > 1) return -1;
// Otherwise, return the height of this subtree as max(leftHeight, rightHight) + 1...
return Math.max(leftHeight, rightHight) + 1;
}
}
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 binary tree
O(N)
, because we need to visit each node in the binary treeO(1)
because we exclude the recursive call stack. The recursive call stack may cost O(N)
we need to account for unbalanced tree. In the general case O(logN)
for balanced tree.