Skip to content

Practice Find Elements in a Contaminated Binary Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#1261
LeetCode Problem

Find Elements in a Contaminated Binary Tree

Problem Statement

A tree's shape remains intact but every value is -1. FindElements(root) recovers values: root is zero; the left child of x is 2*x+1 and the right child is 2*x+2. find(target) reports whether that recovered value exists. Missing children remain missing. Practice approach: Traverse the actual nodes while computing their recovered values and store them in a set. Construction is O(n), with expected O(1) lookup and O(n) storage. Alternatively derive the target's root-to-node path from target+1 and follow existing pointers. Do not assume the tree is complete.

Source: Find Elements in a Contaminated Binary Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
["FindElements","find","find","find"]
[[[-1,-1,-1,null,-1]],[4],[3],[2]]
Output
[null,true,false,true]
Explanation

The level-order shape recovers to [0,1,2,null,4]. Value 4 exists but the missing left child prevents value 3.

Constraints

Every initial node value is -1.
Tree height is at most 20.
1 <= node count, find call count <= 10^4
0 <= target <= 10^6

Related LeetCode Problems

Practice more problems similar to Find Elements in a Contaminated Binary Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.