|
| 1 | +<!--|This file generated by command(leetcode description); DO NOT EDIT. |--> |
| 2 | +<!--+----------------------------------------------------------------------+--> |
| 3 | +<!--|@author openset <[email protected]> |--> |
| 4 | +<!--|@link https://github.com/openset |--> |
| 5 | +<!--|@home https://github.com/openset/leetcode |--> |
| 6 | +<!--+----------------------------------------------------------------------+--> |
| 7 | + |
| 8 | +[< Previous](https://github.com/openset/leetcode/tree/master/problems/shift-2d-grid "Shift 2D Grid") |
| 9 | + |
| 10 | +[Next >](https://github.com/openset/leetcode/tree/master/problems/greatest-sum-divisible-by-three "Greatest Sum Divisible by Three") |
| 11 | + |
| 12 | +## [1261. Find Elements in a Contaminated Binary Tree (Medium)](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree "在受污染的二叉树中查找元素") |
| 13 | + |
| 14 | +<p>Given a binary tree with the following rules:</p> |
| 15 | + |
| 16 | +<ol> |
| 17 | + <li><code>root.val == 0</code></li> |
| 18 | + <li>If <code>treeNode.val == x</code> and <code>treeNode.left != null</code>, then <code>treeNode.left.val == 2 * x + 1</code></li> |
| 19 | + <li>If <code>treeNode.val == x</code> and <code>treeNode.right != null</code>, then <code>treeNode.right.val == 2 * x + 2</code></li> |
| 20 | +</ol> |
| 21 | + |
| 22 | +<p>Now the binary tree is contaminated, which means all <code>treeNode.val</code> have been changed to <code>-1</code>.</p> |
| 23 | + |
| 24 | +<p>You need to first recover the binary tree and then implement the <code>FindElements</code> class:</p> |
| 25 | + |
| 26 | +<ul> |
| 27 | + <li><code>FindElements(TreeNode* root)</code> Initializes the object with a contamined binary tree, you need to recover it first.</li> |
| 28 | + <li><code>bool find(int target)</code> Return if the <code>target</code> value exists in the recovered binary tree.</li> |
| 29 | +</ul> |
| 30 | + |
| 31 | +<p> </p> |
| 32 | +<p><strong>Example 1:</strong></p> |
| 33 | + |
| 34 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/11/06/untitled-diagram-4-1.jpg" style="width: 320px; height: 119px;" /></strong></p> |
| 35 | + |
| 36 | +<pre> |
| 37 | +<strong>Input</strong> |
| 38 | +["FindElements","find","find"] |
| 39 | +[[[-1,null,-1]],[1],[2]] |
| 40 | +<strong>Output</strong> |
| 41 | +[null,false,true] |
| 42 | +<strong>Explanation</strong> |
| 43 | +FindElements findElements = new FindElements([-1,null,-1]); |
| 44 | +findElements.find(1); // return False |
| 45 | +findElements.find(2); // return True </pre> |
| 46 | + |
| 47 | +<p><strong>Example 2:</strong></p> |
| 48 | + |
| 49 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/11/06/untitled-diagram-4.jpg" style="width: 400px; height: 198px;" /></strong></p> |
| 50 | + |
| 51 | +<pre> |
| 52 | +<strong>Input</strong> |
| 53 | +["FindElements","find","find","find"] |
| 54 | +[[[-1,-1,-1,-1,-1]],[1],[3],[5]] |
| 55 | +<strong>Output</strong> |
| 56 | +[null,true,true,false] |
| 57 | +<strong>Explanation</strong> |
| 58 | +FindElements findElements = new FindElements([-1,-1,-1,-1,-1]); |
| 59 | +findElements.find(1); // return True |
| 60 | +findElements.find(3); // return True |
| 61 | +findElements.find(5); // return False</pre> |
| 62 | + |
| 63 | +<p><strong>Example 3:</strong></p> |
| 64 | + |
| 65 | +<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/11/07/untitled-diagram-4-1-1.jpg" style="width: 306px; height: 274px;" /></strong></p> |
| 66 | + |
| 67 | +<pre> |
| 68 | +<strong>Input</strong> |
| 69 | +["FindElements","find","find","find","find"] |
| 70 | +[[[-1,null,-1,-1,null,-1]],[2],[3],[4],[5]] |
| 71 | +<strong>Output</strong> |
| 72 | +[null,true,false,false,true] |
| 73 | +<strong>Explanation</strong> |
| 74 | +FindElements findElements = new FindElements([-1,null,-1,-1,null,-1]); |
| 75 | +findElements.find(2); // return True |
| 76 | +findElements.find(3); // return False |
| 77 | +findElements.find(4); // return False |
| 78 | +findElements.find(5); // return True |
| 79 | +</pre> |
| 80 | + |
| 81 | +<p> </p> |
| 82 | +<p><strong>Constraints:</strong></p> |
| 83 | + |
| 84 | +<ul> |
| 85 | + <li><code>TreeNode.val == -1</code></li> |
| 86 | + <li>The height of the binary tree is less than or equal to <code>20</code></li> |
| 87 | + <li>The total number of nodes is between <code>[1, 10^4]</code></li> |
| 88 | + <li>Total calls of <code>find()</code> is between <code>[1, 10^4]</code></li> |
| 89 | + <li><code>0 <= target <= 10^6</code></li> |
| 90 | +</ul> |
| 91 | + |
| 92 | +### Related Topics |
| 93 | + [[Tree](https://github.com/openset/leetcode/tree/master/tag/tree/README.md)] |
| 94 | + [[Hash Table](https://github.com/openset/leetcode/tree/master/tag/hash-table/README.md)] |
| 95 | + |
| 96 | +### Hints |
| 97 | +<details> |
| 98 | +<summary>Hint 1</summary> |
| 99 | +Use DFS to traverse the binary tree and recover it. |
| 100 | +</details> |
| 101 | + |
| 102 | +<details> |
| 103 | +<summary>Hint 2</summary> |
| 104 | +Use a hashset to store TreeNode.val for finding. |
| 105 | +</details> |
0 commit comments