Coral Count - codepath/compsci_guides GitHub Wiki
Unit 8 Session 1 Standard (Click for link to problem statements)
Problem Highlights
- 💡 Difficulty: Easy
- ⏰ Time to complete: 10 mins
- 🛠️ Topics: Binary Tree, Tree Traversal, Recursion
1: U-nderstand
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?
- What does each node represent in the tree?
- Each node represents a coral formation in the reef.
- How should the function behave if the tree is empty?
- The function should return 0 if the tree is empty.
HAPPY CASE
Input: Binary tree with nodes ["Staghorn", "Sea Fan", "Sea Whip", "Bubble", "Table", "Star", "Fire"]
Output: 7
Explanation: The tree has 7 nodes, so the output is 7.
EDGE CASE
Input: Binary tree with only one node ["Fire"]
Output: 1
Explanation: The tree only has a root, so the output is 1.
2: M-atch
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 Tree Counting problems, we want to consider the following approaches:
- Binary Tree Traversal: Traverse the tree to count each node, which represents a coral.
- Recursion: Use recursion to traverse and count nodes in the tree.
3: P-lan
Plan the solution with appropriate visualizations and pseudocode.
General Idea: Traverse the tree, counting each node (coral) along the way.
1) If the current node is None, return 0.
2) Recursively count the nodes in the left subtree.
3) Recursively count the nodes in the right subtree.
4) Add 1 for the current node and return the total count.
⚠️ Common Mistakes
- Not correctly handling the base case where the tree is empty.
- Miscounting the nodes by not adding the current node's count.
4: I-mplement
Implement the code to solve the algorithm.
class TreeNode:
def __init__(self, value, left=None, right=None):
self.val = value
self.left = left
self.right = right
def count_coral(root):
# Base case: if the current node is None, return 0
if root is None:
return 0
# Recursively count the nodes in the left and right subtrees
left_count = count_coral(root.left)
right_count = count_coral(root.right)
# Add 1 for the current node and return the total count
return 1 + left_count + right_count
5: R-eview
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
- Test with the example given:
- Input 1: Binary tree with nodes ["Staghorn", "Sea Fan", "Sea Whip", "Bubble", "Table", "Star", "Fire"]
- Expected Output: 7
- Input 2: Binary tree with nodes ["Fire", "Black", "Star", "Lettuce", "Sea Whip"]
- Expected Output: 5
- Verify that the outputs match the expected results.
6: E-valuate
Evaluate the performance of your algorithm and state any strong/weak or future potential work.
Assume N
represents the number of nodes in the binary tree.
- Time Complexity:
O(N)
because the algorithm visits each node once. - Space Complexity:
O(H)
whereH
is the height of the tree, due to the recursive call stack.