Find Elements in a Contaminated Binary Tree - Problem
Binary Tree Recovery Challenge

Imagine you have a perfectly structured binary tree with a beautiful mathematical pattern, but it has been contaminated - all values have been changed to -1! Your mission is to recover the original tree and implement an efficient search mechanism.

The original tree follows these recovery rules:
• Root starts at 0
• Left child of node with value x gets 2*x + 1
• Right child of node with value x gets 2*x + 2

You need to implement a FindElements class that:
1. Recovers the contaminated binary tree in the constructor
2. Searches for target values efficiently with the find(target) method

Example tree structure:
       0
/ \
1 2
/ \ / \
3 4 5 6

Input & Output

example_1.py — Basic Tree Recovery
$ Input: Tree: [null,null,null] (contaminated), Operations: ["FindElements","find","find"] Arguments: [[root],[1],[2]]
Output: [null,false,false]
💡 Note: Single root node gets value 0, so find(1) and find(2) both return false since only 0 exists in the recovered tree.
example_2.py — Complete Binary Tree
$ Input: Tree: [-1,-1,-1,-1,-1] (contaminated), Operations: ["FindElements","find","find","find","find"] Arguments: [[root],[1],[3],[5],[2]]
Output: [null,true,true,false,true]
💡 Note: Recovered tree has values [0,1,2,3,4]. find(1)=true, find(3)=true, find(5)=false (doesn't exist), find(2)=true.
example_3.py — Edge Case Single Node
$ Input: Tree: [-1] (contaminated), Operations: ["FindElements","find","find"] Arguments: [[root],[0],[1]]
Output: [null,true,false]
💡 Note: Single node tree recovers to just [0]. find(0)=true (root exists), find(1)=false (no children).

Constraints

  • The number of nodes in the tree is in the range [1, 104]
  • At most 104 calls will be made to find
  • All original tree values are distinct and in the range [0, 106]
  • Tree structure follows the mathematical pattern exactly

Visualization

Tap to expand
Binary Tree Recovery ProcessContaminated-1↓ Recovery0-1 → 2*0+11-1 → 2*0+22Search Index (Hash Set)Step 1: Add 0 ✓Step 2: Add 1 ✓Step 3: Add 2 ✓Step 4: Add 3 ...Step 5: Add 4 ...Continue for all nodes...Query Performancefind(2): hash_set.contains(2) → truefind(7): hash_set.contains(7) → falseTime complexity: O(1) per queryMathematical Pattern:• Root = 0• Left child = 2 × parent + 1• Right child = 2 × parent + 2
Understanding the Visualization
1
Start Recovery
Begin at contaminated root (-1), assign correct value (0), add to search index
2
Process Children
For each node, calculate children values using 2*x+1 and 2*x+2 formula
3
Build Index
As we recover each node, immediately add its value to our hash set
4
Fast Search
All search queries become instant hash lookups in our pre-built index
Key Takeaway
🎯 Key Insight: Since we must traverse every node to recover the tree anyway, building our search index during the same traversal gives us O(1) queries with no extra tree traversals!
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
28.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen