Building an Underwater Kingdom - codepath/compsci_guides GitHub Wiki

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

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 15 mins
  • 🛠️ Topics: Binary Tree, Tree Construction

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 is the structure of the binary tree?
    • The binary tree is structured as depicted in the diagram with "Poseidon" as the root, "Atlantis" and "Oceania" as the children, and "Coral", "Pearl", "Kelp", and "Reef" as the grandchildren.
HAPPY CASE
Input: Binary Tree as described in the problem
Output: Poseidon, Atlantis, Oceania, Coral, Pearl, Kelp, Reef
Explanation: The binary tree has been constructed according to the given structure.

EDGE CASE
Input: A binary tree with no children
Output: Just the root node "Poseidon"
Explanation: The binary tree consists only of the root node, with no children or grandchildren.

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

  • Binary Tree Construction: Create the tree by assigning left and right children to each node as specified in the problem statement.
  • Tree Traversal: Ensure the tree structure is correct by printing or traversing the tree.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Construct the binary tree by assigning children to each node according to the diagram provided.

1) Start with the root node "Poseidon".
2) Assign "Atlantis" as the left child of "Poseidon".
3) Assign "Oceania" as the right child of "Poseidon".
4) Assign "Coral" and "Pearl" as the left and right children of "Atlantis", respectively.
5) Assign "Kelp" and "Reef" as the left and right children of "Oceania", respectively.
6) The binary tree is now constructed as depicted in the problem.

⚠️ Common Mistakes

  • Forgetting to assign one of the children to a node, resulting in an incomplete tree.
  • Misplacing a node, leading to incorrect tree structure.

4: I-mplement

Implement the code to solve the algorithm.

root = TreeNode("Poseidon")
root.left = TreeNode("Atlantis")
root.right = TreeNode("Oceania")
root.left.left = TreeNode("Coral")
root.left.right = TreeNode("Pearl")
root.right.left = TreeNode("Kelp")
root.right.right = TreeNode("Reef")

5: R-eview

Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.

  • Trace through the tree structure to ensure all nodes are placed correctly:
    • Root: "Poseidon"
    • Left child of "Poseidon": "Atlantis"
    • Right child of "Poseidon": "Oceania"
    • Children of "Atlantis": "Coral" (left), "Pearl" (right)
    • Children of "Oceania": "Kelp" (left), "Reef" (right)

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(1) because we are simply assigning references to the nodes.
  • Space Complexity: O(1) as we are not using any additional data structures, just creating the nodes.