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?
HAPPY CASE
Input: 1
/ \
2 3
\
5
Output: ["1->2->5", "1->3"]
Input: 1
/ \
2 3
\ \
5 2
Output: ["1->2->5", "1->3->2"]
EDGE CASE (Only Root Node)
Input: 1
Output: ["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.
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.
SOLUTION 1:
General Idea: Pre-Order traversal through the Binary Tree while keeping track of a current path. When we reach a leaf node, store the current path and pop elements off as we backtrack and explore more of the tree.
0) Create helper function to allow us to retain memory of allPaths
1) Basecase: If the current element is Null, return
2) Recursive: Build Current Path
a) Add the current node to the current path data structure
b) If the current node is a leaf node, store the current path in the tree
c) Recurse Left and Recurse Right
d) Pop the current node off the current path, since we will no longer have more root-to-leaf paths that go through the current node.
3) Create allPaths array to retain memory of allPaths
4) Run helper function to collect allPaths
5) Return allPaths
SOLUTION 2:
General Idea: Lets build the paths as we progress through the nodes. If the node is a leaf node, then add it to our results.
1. Create a helper function to recursively progress through the nodes.
a. Collect the values and build string
b. At a leaf node add built string to results
c. Progress to left node and right node
2. Create results array
3. Call helper function to build results
4. Return results
⚠️ Common Mistakes
Implement the code to solve the algorithm.
Solution 1
class Solution:
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
# Create helper function to allow us to retain memory of allPaths
def helper(root, currPath):
# Basecase: If the current element is Null, return
if not root:
return
# Recusive: Build Current Path
# Add the current node to the current path data structure
currPath.append(str(root.val))
# If the current node is a leaf node, store the current path in the tree
if not root.left and not root.right:
allPaths.append("->".join(currPath))
# Recurse Left and Recurse Right
helper(root.left, currPath)
helper(root.right, currPath)
# Pop the current node off the current path, since we will no longer have more root-to-leaf paths that go through the current node.
currPath.pop()
# Create allPaths array to retain memory of allPaths
allPaths = []
# Run helper function to collect allPaths
helper(root, [])
# Return allPaths
return allPaths
Solution 2
# 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 binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
# Create a helper function to recursively progress through the nodes.
def dfs(node, s):
# Collect the values and build string
if s != "":
s += "->"
s += str(node.val)
# At a leaf node add built string to results
if not node.left and not node.right:
res.append(s)
# Progress to left node and right node
if node.left:
dfs(node.left, s)
if node.right:
dfs(node.right, s)
# Create results array
res = []
# Call helper function to build results
dfs(root, "")
# Return results
return res
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
// Create results array
List<String> answer = new ArrayList<String>();
// Call helper function to build results
if (root != null) searchBT(root, "", answer);
// Return results
return answer;
}
// Create a helper function to recursively progress through the nodes
private void searchBT(TreeNode root, String path, List<String> answer) {
// Collect the values and build string
// At a leaf node add built string to results
if (root.left == null && root.right == null) answer.add(path + root.val);
// Progress to left node and right node
if (root.left != null) searchBT(root.left, path + root.val + "->", answer);
if (root.right != null) searchBT(root.right, path + root.val + "->", answer);
}
}
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 tree
Solutions 1 & 2
O(N)
because we need to visit each node in binary tree.O(N)
because we need to create a results array of all root-to-left paths which is (n+1)/2
leafs.