|
| 1 | +<p>Given a binary tree with the following rules:</p> |
| 2 | + |
| 3 | +<ol> |
| 4 | + <li><code>root.val == 0</code></li> |
| 5 | + <li>For any <code>treeNode</code>: |
| 6 | + <ol type="a"> |
| 7 | + <li>If <code>treeNode.val</code> has a value <code>x</code> and <code>treeNode.left != null</code>, then <code>treeNode.left.val == 2 * x + 1</code></li> |
| 8 | + <li>If <code>treeNode.val</code> has a value <code>x</code> and <code>treeNode.right != null</code>, then <code>treeNode.right.val == 2 * x + 2</code></li> |
| 9 | + </ol> |
| 10 | + </li> |
| 11 | +</ol> |
| 12 | + |
| 13 | +<p>Now the binary tree is contaminated, which means all <code>treeNode.val</code> have been changed to <code>-1</code>.</p> |
| 14 | + |
| 15 | +<p>Implement the <code>FindElements</code> class:</p> |
| 16 | + |
| 17 | +<ul> |
| 18 | + <li><code>FindElements(TreeNode* root)</code> Initializes the object with a contaminated binary tree and recovers it.</li> |
| 19 | + <li><code>bool find(int target)</code> Returns <code>true</code> if the <code>target</code> value exists in the recovered binary tree.</li> |
| 20 | +</ul> |
| 21 | + |
| 22 | +<p> </p> |
| 23 | +<p><strong class="example">Example 1:</strong></p> |
| 24 | +<img alt="" src="https://assets.leetcode.com/uploads/2019/11/06/untitled-diagram-4-1.jpg" style="width: 320px; height: 119px;" /> |
| 25 | +<pre> |
| 26 | +<strong>Input</strong> |
| 27 | +["FindElements","find","find"] |
| 28 | +[[[-1,null,-1]],[1],[2]] |
| 29 | +<strong>Output</strong> |
| 30 | +[null,false,true] |
| 31 | +<strong>Explanation</strong> |
| 32 | +FindElements findElements = new FindElements([-1,null,-1]); |
| 33 | +findElements.find(1); // return False |
| 34 | +findElements.find(2); // return True </pre> |
| 35 | + |
| 36 | +<p><strong class="example">Example 2:</strong></p> |
| 37 | +<img alt="" src="https://assets.leetcode.com/uploads/2019/11/06/untitled-diagram-4.jpg" style="width: 400px; height: 198px;" /> |
| 38 | +<pre> |
| 39 | +<strong>Input</strong> |
| 40 | +["FindElements","find","find","find"] |
| 41 | +[[[-1,-1,-1,-1,-1]],[1],[3],[5]] |
| 42 | +<strong>Output</strong> |
| 43 | +[null,true,true,false] |
| 44 | +<strong>Explanation</strong> |
| 45 | +FindElements findElements = new FindElements([-1,-1,-1,-1,-1]); |
| 46 | +findElements.find(1); // return True |
| 47 | +findElements.find(3); // return True |
| 48 | +findElements.find(5); // return False</pre> |
| 49 | + |
| 50 | +<p><strong class="example">Example 3:</strong></p> |
| 51 | +<img alt="" src="https://assets.leetcode.com/uploads/2019/11/07/untitled-diagram-4-1-1.jpg" style="width: 306px; height: 274px;" /> |
| 52 | +<pre> |
| 53 | +<strong>Input</strong> |
| 54 | +["FindElements","find","find","find","find"] |
| 55 | +[[[-1,null,-1,-1,null,-1]],[2],[3],[4],[5]] |
| 56 | +<strong>Output</strong> |
| 57 | +[null,true,false,false,true] |
| 58 | +<strong>Explanation</strong> |
| 59 | +FindElements findElements = new FindElements([-1,null,-1,-1,null,-1]); |
| 60 | +findElements.find(2); // return True |
| 61 | +findElements.find(3); // return False |
| 62 | +findElements.find(4); // return False |
| 63 | +findElements.find(5); // return True |
| 64 | +</pre> |
| 65 | + |
| 66 | +<p> </p> |
| 67 | +<p><strong>Constraints:</strong></p> |
| 68 | + |
| 69 | +<ul> |
| 70 | + <li><code>TreeNode.val == -1</code></li> |
| 71 | + <li>The height of the binary tree is less than or equal to <code>20</code></li> |
| 72 | + <li>The total number of nodes is between <code>[1, 10<sup>4</sup>]</code></li> |
| 73 | + <li>Total calls of <code>find()</code> is between <code>[1, 10<sup>4</sup>]</code></li> |
| 74 | + <li><code>0 <= target <= 10<sup>6</sup></code></li> |
| 75 | +</ul> |
0 commit comments