Leetcode Problem 1261. Find Elements in a Contaminated Binary Tree

1261. Find Elements in a Contaminated Binary Tree

Leetcode Solutions

Recovering and Searching in a Contaminated Binary Tree

  1. Initialize the FindElements class with the contaminated root node.
  2. Recover the tree by setting the root value to 0 and recursively applying the rules to set the children's values.
  3. Implement the find method to search for the target value in the tree.
    • Start from the root and compare the current node's value with the target.
    • If the target is found, return true.
    • If the target is less than the current node's value, search in the left subtree.
    • If the target is greater, search in the right subtree.
    • If the node is null, return false.
UML Thumbnail

Using HashSet for Recovery and Search

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...