Unit 8 Session 1 Standard (Click for link to problem statements)
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?
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.
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:
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
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")
Review the code by running specific example(s) and recording values (watchlist) of your code's variables along the way.
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.
O(1)
because we are simply assigning references to the nodes.O(1)
as we are not using any additional data structures, just creating the nodes.