Codepath

Ocean Layers

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 the depth of the tree represent?
    • The depth of the tree is the number of nodes on the longest path from the root node to a leaf node.
  • 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 ["Sunlight", "Twilight", "Abyss", "Trenches", "Anglerfish", "Squid", "Giant Squid"]
Output: 4
Explanation: The longest path is ["Sunlight", "Twilight", "Abyss", "Trenches"], so the depth is 4.

EDGE CASE
Input: Binary tree with only one node ["Sunlight"]
Output: 1
Explanation: The tree has only the root, so the depth 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 Depth problems, we want to consider the following approaches:

  • Binary Tree Traversal: Traverse the tree to calculate the depth.
  • Recursion: Use recursion to determine the depth of the tree by calculating the depth of each subtree.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Traverse the tree recursively, calculating the depth of the left and right subtrees, and return the maximum depth.

1) If the current node is None, return 0.
2) Recursively calculate the depth of the left subtree.
3) Recursively calculate the depth of the right subtree.
4) The depth of the current node is 1 plus the maximum of the depths of the left and right subtrees.
5) Return the depth of the tree rooted at the current node.

⚠️ Common Mistakes

  • Not correctly handling the base case where the tree is empty.
  • Miscalculating the depth by not adding the current node's level.

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 ocean_depth(root):
    # Base case: if the tree is empty, the depth is 0
    if root is None:
        return 0
    
    # Recursive case: calculate the depth of left and right subtrees
    left_depth = ocean_depth(root.left)
    right_depth = ocean_depth(root.right)
    
    # The depth of the tree rooted at the current node
    return 1 + max(left_depth, right_depth)

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 ["Sunlight", "Twilight", "Abyss", "Trenches", "Anglerfish", "Squid", "Giant Squid"]
    • Expected Output: 4
    • Input 2: Binary tree with nodes ["Spray Zone", "Beach", "High Tide", "Middle Tide", "Low Tide"]
    • Expected Output: 4
    • 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) where H is the height of the tree, due to the recursive call stack.
Fork me on GitHub