Skip to content

Grafting Apples

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

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

Problem Highlights

  • 💡 Difficulty: Easy
  • Time to complete: 15 mins
  • 🛠️ Topics: Binary Tree, 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 is the structure of the binary tree?
    • The binary tree is structured as depicted in the diagram with "Trunk" as the root, "Mcintosh" and "GrannySmith" as the children, and "Fuji", "Opal", "Crab", and "Gala" as the grandchildren.
HAPPY CASE
Input: Binary Tree as described in the problem
Output: Trunk, Mcintosh, GrannySmith, Fuji, Opal, Crab, Gala
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 "Trunk"
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 "Trunk".
2) Assign "Mcintosh" as the left child of "Trunk".
3) Assign "GrannySmith" as the right child of "Trunk".
4) Assign "Fuji" and "Opal" as the left and right children of "Mcintosh", respectively.
5) Assign "Crab" and "Gala" as the left and right children of "GrannySmith", 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.

# Start with the root node
root = TreeNode("Trunk")

# Root's Children
root.left = TreeNode("Mcintosh")
root.right = TreeNode("GrannySmith")

# Root's Grandchildren
root.left.left = TreeNode("Fuji")
root.left.right = TreeNode("Opal")
root.right.left = TreeNode("Crab")
root.right.right = TreeNode("Gala")

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: "Trunk"
    • Left child of "Trunk": "Mcintosh"
    • Right child of "Trunk": "GrannySmith"
    • Children of "Mcintosh": "Fuji" (left), "Opal" (right)
    • Children of "GrannySmith": "Crab" (left), "Gala" (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.
Clone this wiki locally