Find Elements in a Contaminated Binary Tree — Algorithm Visualization & Coding Challenge

Choose Your Learning Path

How would you like to learn today?
Visualize algorithms in real time, explore them step by step, or challenge yourself with a test.Choose a path to focus—or scroll down to preview all options.

🧠 Active Learning

Visualize the algorithm step-by-step with interactive animations in real time.

📖 Passive Learning

Read the full explanation, examples, and starter code at your own pace.

🎯 Challenge Mode

Drag and arrange the algorithm steps in the correct execution order.

🧠 Select Active to activate

JUMP INTO VISUALIZATION
Watch algorithms run step by step.

Follow every state change, comparison, and transformation as the execution unfolds in real time.

📖 Select Passive to activate

Understanding Find Elements in a Contaminated Binary Tree
Detailed explanation and reference materials
Problem Overview

Find Elements in a Contaminated Binary Tree

Problem Statement

You are given a contaminated binary tree where all TreeNode.val have been changed to -1. The tree follows these rules:

  • The root node has a value of 0.
  • If a node has a value x, then:
    • Its left child (if exists) will have a value 2 * x + 1.
    • Its right child (if exists) will have a value 2 * x + 2.

Task

Implement the FindElements class:

  • FindElements(TreeNode* root): Initializes the object with a contaminated binary tree and recovers it.
  • bool find(int target): Returns true if the target value exists in the recovered binary tree, otherwise returns false.

Test Case 1: Single Node Tree

Input
json
["FindElements","find","find"]
[[[-1]],[0],[1]]

tree1

Output
json
[null,true,false]
Explanation
plaintext
FindElements findElements = new FindElements([-1]);
findElements.find(0); // returns True (root is always 0)
findElements.find(1); // returns False (no left or right child exists)

Test Case 2: Complete Binary Tree of Height 3

Input
json
["FindElements","find","find","find","find","find"]
[[[-1,-1,-1,-1,-1,-1,-1]],[3],[5],[6],[7],[8]]

tree1

Output
json
[null,true,true,true,false,false]
Explanation
plaintext
FindElements findElements = new FindElements([-1,-1,-1,-1,-1,-1,-1]);
findElements.find(3); // returns True
findElements.find(5); // returns True
findElements.find(6); // returns True
findElements.find(7); // returns False (no node with value 7 exists)
findElements.find(8); // returns False (no node with value 8 exists)

Constraints

  • TreeNode.val=1TreeNode.val = -1
  • The height of the binary tree is 20\leq 20.
  • The total number of nodes is in the range [1,104][1, 10^4].
  • find()find() will be called at most 10410^4 times.
  • 0target1060 \leq target \leq 10^6.

Approach

  1. Recover the tree using DFS or BFS:
    • Start from the root and assign it 0.
    • For each node x, assign its left child 2 * x + 1 and right child 2 * x + 2.
    • Store these values in a set or unordered_set for quick lookup.
  2. Find operation in O(1):
    • Use a hash set to store valid node values for O(1) lookup in the find() function.

— Written by Saurabh Patil • B.Tech CSE • Software Developer

Categories
leetcode-problem-of-the-day
binary-trees
java
Reference Link
https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/description/

Loading component...

Starter Code
Test, modify, or copy the starter code. Click "Visualize" to import into the canvas.
Java
Output:
Understood Algorithm, Test Me now 🎮

🎯 Select Challenge to activate

🧠 Logic Puzzle
Think & Arrange, Don't Just Copy-Paste

Drag and arrange the algorithm steps in the correct execution order instead of spending time typing code letter by letter.

Arrange the Algorithm Correctly 🧩

The algorithm is divided into three logical parts. Carefully rearrange each section in the correct order to form a complete and valid solution.

Understand Below Algorithm

Don't Know Current Algorithm ?  

Green text means the instruction is placed in the correct position.

Red text means the instruction is in the wrong position.

Block Colors

Instructions with the same background color indicate particular blocks start and end.

A tick mark means the instruction is correct and locked.

🔒 Locked steps cannot be moved. Only unlocked steps are draggable.

🔊 Enable sound for swap feedback and completion effects.

DrawToCode — Visualize, Practice & Master Algorithms

Learn data structures and algorithms through interactive visualizations. Practice coding problems, track your progress, and understand concepts deeply.

EmailLinkedInTwitterInstagramGitHub
© 2026 DrawToCode. All rights reserved.