Problem Description
You are given a contaminated binary tree where every node’s value is -1. The original tree was constructed with the rule that the root is 0, and for any node with value x, its left child is 2 * x + 1 and its right child is 2 * x + 2 (if they exist). Your task is to recover the tree and implement a method called find(int target) to check if a given target value exists in the recovered tree.
Key Insights
- Recover the binary tree using either depth-first search (DFS) or breadth-first search (BFS).
- Each node’s original value can be computed based on its parent's value using the formulas: left child = 2 * parent's value + 1, right child = 2 * parent's value + 2.
- Store the recovered node values in a HashSet for O(1) lookup during find queries.
- Preprocessing the tree (recovering and populating the set) takes O(n) time, while each find operation is O(1).
Space and Time Complexity
Time Complexity:
- Initialization (recovering the tree): O(n) where n is the total number of nodes.
- Find queries: O(1) per query.
Space Complexity:
- O(n) for storing node values in a set, plus O(n) for the recursion/queue in DFS/BFS, which simplifies to O(n).
Solution
The solution consists of two primary steps:
- Recover the contaminated binary tree: Starting from the root (set to 0), traverse the tree using DFS or BFS. For each node, compute and assign its correct value based on the given formulas. As you traverse, record each computed node value in a HashSet.
- Implement the find(target) method: Simply check whether the target exists in the HashSet. This provides a fast O(1) lookup.
The key trick is to preprocess the tree once and reuse the stored values for efficient lookups. This method avoids repeated recomputation and makes the implementation simple and efficient.