Leetcode 1261: Find Elements in a Contaminated Binary Tree

grid47
grid47
Exploring patterns and algorithms
Jul 3, 2024 5 min read

Given a contaminated binary tree, recover it and implement a class to search for specific values in the tree.
Problem
Approach
Steps
Complexity
Input: The input is a list of operations. Each operation is either the initialization of a tree or a search for a target value.
Example: [[[-1, null, -1]], [1], [2]]
Constraints:
• The tree has at most 10^4 nodes.
• Each node value is -1 initially.
Output: The output is a list where each entry corresponds to the result of a 'find' operation, indicating whether the target exists in the tree.
Example: [null, false, true]
Constraints:
• The result of the 'find' operation is either true or false.
Goal: Recover the tree's original values and search for specific targets efficiently.
Steps:
• 1. Initialize the tree with the given contaminated root.
• 2. Recover the values of each node using depth-first traversal.
• 3. Implement the 'find' operation by checking if the target exists in the recovered set of values.
Goal: The constraints for the problem involve tree size and the values to search for.
Steps:
• 1 <= tree size <= 10^4
• Target value is between 0 and 10^6
Assumptions:
• The input tree has been contaminated (all node values are initially -1).
• Each node follows the given value rules in the original uncontaminated tree.
Input: [[[-1, null, -1]], [1], [2]]
Explanation: In this example, after recovering the tree, the result of searching for 1 is 'false' and 2 is 'true'.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus