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:
root =
[1,null,0,0,1]
Output:
[1,null,0,null,1]
Input:
root =
[1,0,1,0,0,0,1]
Output:
[1,null,1,null,1]
EDGE CASE
Input: [0]
/ \
[0] [0]
/ \ / \
[0] [0] [0] [0]
/ / / \ \
[0] [0] [0] [0] [0]
Output: Since there are no 1's in the entire tree, we should remove all nodes.
In this case, we should return NULL (this would be a good clarifying question to ask your interviewer).
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 trees, some things we should consider are:
Plan the solution with appropriate visualizations and pseudocode.
Traverse the tree via DFS, recursively prune left and right subtrees, then return information regarding whether or not current subtree should be pruned.
1) Traverse the tree via DFS
2) As we traverse, we want to determine if the current node is the root of a subtree containing a 1
3) We can recursively prune the left and right subtrees of the current node,
and then return information about whether or not the pruned left/right subtree contains a 1 back to the current node
4) If the left/right subtree does not contain a 1,
it should be removed from the tree (by setting the current node's left/right child to NULL)
5) Then, if the node's value is 1 or one of its subtrees contains a 1,
we know to return true back to the parent node
⚠️ Common Mistakes
Implement the code to solve the algorithm.
class Solution {
public TreeNode pruneTree(TreeNode root) {
return containsOne(root) ? root : null;
}
public boolean containsOne(TreeNode node) {
if (node == null) return false;
// check if any node in the left subtree contains a 1.
boolean leftContainsOne = containsOne(node.left);
// check if any node in the right subtree contains a 1.
boolean rightContainsOne = containsOne(node.right);
// if the left subtree does not contain a 1, prune the subtree.
if (!leftContainsOne) node.left = null;
// if the right subtree does not contain a 1, prune the subtree.
if (!rightContainsOne) node.right = null;
// return true if the current node, its left or right subtree contains a 1.
return node.val == 1 || leftContainsOne || rightContainsOne;
}
}
def pruneTree(self, root):
def pruneHelper(node):
# assume that leftSubTree does not contain 1
leftSubTreeContains1 = False
# if there is a left child, then the left subtree could have a 1, so recursively check this
if node.left:
leftSubTreeContains1 = pruneHelper(node.left)
# assume that rightSubTree does not contain 1
rightSubTreeContains1 = False
# if there is a right child, then the right subtree could have a 1, so recursively check this
if node.right:
rightSubTreeContains1 = pruneHelper(node.right)
# if the leftSubTree does not have a 1, it should be removed, as per the algorithm specification
if not leftSubTreeContains1:
node.left = None
# if the rightSubTree does not have a 1, it should be removed, as per the algorithm specification
if not rightSubTreeContains1:
node.right = None
# the subtree rooted at this node contains a 1 if:
# 1) the root of the subtree (the current node) has value 1
# 2) the leftSubTree contains a 1
# 3) the rightSubTree contains a 1
# The parent node will use this return value to determine if its child should be pruned or not
return node.value == 1 or leftSubTreeContains1 or rightSubTreeContains1
# calling pruneHelper on the root tells us if the tree has a 1 or not
treeHas1 = pruneHelper(root)
# if the tree does not contain a 1, then all nodes (including the root) should be removed
if not treeHas1:
return None
# otherwise, return the pruned tree
return root
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.
Time Complexity: O(N)
, where N
is the number of nodes in the tree. We process each node once.
Space Complexity: O(N)
, the recursion call stack can be as large as the height of the tree.