Unit 8 Session 1 (Click for link to problem statements)
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: TreeNode(10, TreeNode(5), TreeNode(15, TreeNode(12), TreeNode(20))), value = 13
Output: True
Explanation: The value 20 is greater than 13 and exists in the tree.
EDGE CASE
Input: TreeNode(10, TreeNode(5), TreeNode(8)), value = 10
Output: False
Explanation: There is no node with a value greater than 10 in the tree.
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.
This problem is a tree search challenge where the goal is to find if any node contains a value greater than a specified number. It resembles classic search operations but with a conditional twist to check values against a target.
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the tree and check if any node has a value greater than the given value.
1) Start at the root.
2) If the current node is None, return False.
3) If the current node's value is greater than the given value, return True.
4) Recursively check both the left and right subtrees for a greater value.
⚠️ Common Mistakes
Implement the code to solve the algorithm.
def contains_greater(node, value):
"
Checks if any node in the binary tree rooted at `node` contains a value greater than `value`.
"
if node is None:
return False
if node.val > value:
return True
# Recursively check the left and right subtrees
return contains_greater(node.left, value) or contains_greater(node.right, value)
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.
O(n)
where n is the number of nodes in the tree, as the worst case requires visiting every node.O(h)
where h is the height of the tree, due to recursion. This could be O(n)
in a highly skewed tree.