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)
and Space is O(N)
.HAPPY CASE
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
Input: root = [1]
Output: [[1]]
EDGE CASE
Input: root = []
Output: []
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: Process the binary tree by level using a queue to repeatedly store and process all nodes from previous layer in queue.
1. Create a results array to store each level.
2. Create a queue with root node and process until no more nodes in queue
a. Process the current number of nodes in queue to exclude new nodes added to queue.
b. Store value of node into current level and add children from each node to queue, to be processed in next level.
c. Store level into results.
3. Return results.
⚠️ Common Mistakes
Choosing a wrong traversal type
Failing to recognize the need to store results during the processing of nodes.
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 levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
# Create a results array to store each level.
results = []
# Create a queue with root node and process until no more nodes in queue
queue = [root]
while queue:
# Process the current number of nodes in queue to exclude new nodes added to queue.
levelCount = len(queue)
level = []
for i in range(levelCount):
# Store value of node into current level and add children from each node to queue, to be processed in next level.
node = queue.pop(0)
if node:
level.append(node.val)
queue.append(node.left)
queue.append(node.right)
# Store level into results.
if level:
results.append(level)
# Return results.
return results
public class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
// Create a results array to store each level.
List<List<Integer>> wrapList = new LinkedList<List<Integer>>();
if(root == null) return wrapList;
// Create a queue with root node and process until no more nodes in queue
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
// Process the current number of nodes in queue to exclude new nodes added to queue
int levelNum = queue.size();
List<Integer> subList = new LinkedList<Integer>();
// Store value of node into current level and add children from each node to queue, to be processed in next level.
for(int i=0; i<levelNum; i++) {
if(queue.peek().left != null) queue.offer(queue.peek().left);
if(queue.peek().right != null) queue.offer(queue.peek().right);
subList.add(queue.poll().val);
}
// Store level into results
wrapList.add(subList);
}
// Return results
return wrapList;
}
}
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 nodes in tree
O(N)
because we need to visit each node in binary tree.O(N)
because we need to store each node's val into results.