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
The original tree follows these recovery rules:
• Root starts at
• Left child of node with value
• Right child of node with value
You need to implement a
1. Recovers the contaminated binary tree in the constructor
2. Searches for target values efficiently with the
Example tree structure:
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 + 2You 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) methodExample 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
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!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code