Skip to content

Uniform Coral

kyra-ptn edited this page Sep 3, 2024 · 2 revisions

Unit 8 Session 1 Advanced (Click for link to problem statements)

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 10 mins
  • 🛠️ Topics: Binary Tree, Recursion, Tree Traversal

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 in the binary tree represent?
    • Each node represents a part of the coral structure with a specific value.
  • What does it mean for the coral to be "uniform"?
    • The coral is uniform if all nodes in the binary tree have the same value.
  • How should the function behave if the tree is empty?
    • The function should return True since an empty tree is trivially uniform.
HAPPY CASE
Input: Binary tree with nodes [1, 1, 1, 1, 1]
Output: True
Explanation: All nodes have the same value, so the coral is uniform.

EDGE CASE
Input: Binary tree with nodes [1, 2, 1]
Output: False
Explanation: The node with value 2 differs from the others, so the coral is not uniform.

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 Uniformity problems, we want to consider the following approaches:

  • Binary Tree Traversal: Traverse the tree to check if all nodes have the same value.
  • Recursion: Use recursion to check uniformity by comparing each node's value with its children's values.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Traverse the tree recursively, checking at each step if the current node has the same value as its children.

1) If the current node is None, return True.
2) If the left child exists and its value is different from the current node's value, return False.
3) If the right child exists and its value is different from the current node's value, return False.
4) Recursively check if the left and right subtrees are uniform.
5) Return True if both the left and right subtrees are uniform.

⚠️ Common Mistakes

  • Not correctly handling the case where the tree is empty.
  • Failing to compare both left and right children with the current node's value.

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 is_uniform(root):
    if root is None:
        return True
    
    if root.left is not None and root.left.val != root.val:
        return False
    if root.right is not None and root.right.val != root.val:
        return False
    
    return is_uniform(root.left) and is_uniform(root.right)

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 examples given:

    - Input 1: Binary tree with nodes [1, 1, 1, 1, 1]
    - Expected Output: True
    
    - Input 2: Binary tree with nodes [1, 2, 1]
    - Expected Output: False
    

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 needs to visit each node in the tree.
  • Space Complexity: O(H) where H is the height of the tree, due to the recursive call stack.
Clone this wiki locally