java.lang.Object
g1201_1300.s1261_find_elements_in_a_contaminated_binary_tree.FindElements

public class FindElements extends Object
1261 - Find Elements in a Contaminated Binary Tree.<p>Medium</p> <p>Given a binary tree with the following rules:</p> <ol> <li><code>root.val == 0</code></li> <li>If <code>treeNode.val == x</code> and <code>treeNode.left != null</code>, then <code>treeNode.left.val == 2 * x + 1</code></li> <li>If <code>treeNode.val == x</code> and <code>treeNode.right != null</code>, then <code>treeNode.right.val == 2 * x + 2</code></li> </ol> <p>Now the binary tree is contaminated, which means all <code>treeNode.val</code> have been changed to <code>-1</code>.</p> <p>Implement the <code>FindElements</code> class:</p> <ul> <li><code>FindElements(TreeNode* root)</code> Initializes the object with a contaminated binary tree and recovers it.</li> <li><code>bool find(int target)</code> Returns <code>true</code> if the <code>target</code> value exists in the recovered binary tree.</li> </ul> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2019/11/06/untitled-diagram-4-1.jpg" alt="" /></p> <p><strong>Input</strong> [&ldquo;FindElements&rdquo;,&ldquo;find&rdquo;,&ldquo;find&rdquo;] [<a href="-1,null,-1">-1,null,-1</a>,[1],[2]]</p> <p><strong>Output:</strong> [null,false,true]</p> <p><strong>Explanation:</strong></p> <pre><code> FindElements findElements = new FindElements([-1,null,-1]); findElements.find(1); // return False findElements.find(2); // return True </code></pre> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2019/11/06/untitled-diagram-4.jpg" alt="" /></p> <p><strong>Input</strong> [&ldquo;FindElements&rdquo;,&ldquo;find&rdquo;,&ldquo;find&rdquo;,&ldquo;find&rdquo;] [<a href="-1,-1,-1,-1,-1">-1,-1,-1,-1,-1</a>,[1],[3],[5]]</p> <p><strong>Output:</strong> [null,true,true,false]</p> <p><strong>Explanation:</strong></p> <pre><code> FindElements findElements = new FindElements([-1,-1,-1,-1,-1]); findElements.find(1); // return True findElements.find(3); // return True findElements.find(5); // return False </code></pre> <p><strong>Example 3:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2019/11/07/untitled-diagram-4-1-1.jpg" alt="" /></p> <p><strong>Input</strong> [&ldquo;FindElements&rdquo;,&ldquo;find&rdquo;,&ldquo;find&rdquo;,&ldquo;find&rdquo;,&ldquo;find&rdquo;] [<a href="-1,null,-1,-1,null,-1">-1,null,-1,-1,null,-1</a>,[2],[3],[4],[5]]</p> <p><strong>Output:</strong> [null,true,false,false,true]</p> <p><strong>Explanation:</strong></p> <pre><code> FindElements findElements = new FindElements([-1,null,-1,-1,null,-1]); findElements.find(2); // return True findElements.find(3); // return False findElements.find(4); // return False findElements.find(5); // return True </code></pre> <p><strong>Constraints:</strong></p> <ul> <li><code>TreeNode.val == -1</code></li> <li>The height of the binary tree is less than or equal to <code>20</code></li> <li>The total number of nodes is between <code>[1, 10<sup>4</sup>]</code></li> <li>Total calls of <code>find()</code> is between <code>[1, 10<sup>4</sup>]</code></li> <li><code>0 <= target <= 10<sup>6</sup></code></li> </ul>
  • Constructor Details

    • FindElements

      public FindElements(TreeNode root)
  • Method Details

    • find

      public boolean find(int target)