Codepath

Poseidon's Decision

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

Problem Highlights

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

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 values can the nodes of the tree hold?
    • Leaf nodes can have boolean values (True or False), while non-leaf nodes can have string values (AND or OR).
  • How should the function behave if the root has no children?
    • The function should return the value of the root node if it has no children.
HAPPY CASE
Input: Binary tree with root "OR" and children "True", "False"
Output: True
Explanation: The evaluation of "OR" with "True" and "False" is `True`.

EDGE CASE
Input: Binary tree with root "False" and no children
Output: False
Explanation: The root is a leaf node with value `False`, so it directly returns `False`.

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

  • Binary Tree Evaluation: Directly evaluate the tree based on the boolean logic represented by the nodes.
  • Conditional Logic: Use conditionals to apply the operations (AND or OR) on the child nodes.

3: P-lan

Plan the solution with appropriate visualizations and pseudocode.

General Idea: Evaluate the boolean expression represented by the binary tree by applying the operation in each non-leaf node to its children.

1) If the root has no children, return its boolean value.
2) If the root has the value "AND", return the logical AND of the evaluations of its left and right children.
3) If the root has the value "OR", return the logical OR of the evaluations of its left and right children.
4) The result of the evaluation is returned as the final decision.

⚠️ Common Mistakes

  • Not handling the case where the root is a leaf node properly.
  • Misinterpreting the AND and OR operations, leading to incorrect evaluations.

4: I-mplement

Implement the code to solve the algorithm.

class TreeNode:
    def __init__(self, value, right=None, left=None):
        self.val = value
        self.right = right
        self.left = left

def get_decision(root):
    # If root node has no children
    if root.left is None and root.right is None:
        return root.val
        
    # Otherwise, apply the operation based on the current node's value
    if root.val == "AND":
        return root.left.val and root.right.val
    elif root.val == "OR":
        return root.left.val or root.right.val

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 root "OR" and children "True", "False"
    • Expected Output: True
    • Input 2: Binary tree with root "False" and no children
    • Expected Output: False
    • 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(1) because the tree has at most three nodes, leading to constant-time evaluation.
  • Space Complexity: O(1) as no additional space is used beyond the input.
Fork me on GitHub