mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
77 lines
3.1 KiB
HTML
77 lines
3.1 KiB
HTML
<p>给出一个满足下述规则的二叉树:</p>
|
||
|
||
<ol>
|
||
<li><code>root.val == 0</code></li>
|
||
<li>如果 <code>treeNode.val == x</code> 且 <code>treeNode.left != null</code>,那么 <code>treeNode.left.val == 2 * x + 1</code></li>
|
||
<li>如果 <code>treeNode.val == x</code> 且 <code>treeNode.right != null</code>,那么 <code>treeNode.right.val == 2 * x + 2</code></li>
|
||
</ol>
|
||
|
||
<p>现在这个二叉树受到「污染」,所有的 <code>treeNode.val</code> 都变成了 <code>-1</code>。</p>
|
||
|
||
<p>请你先还原二叉树,然后实现 <code>FindElements</code> 类:</p>
|
||
|
||
<ul>
|
||
<li><code>FindElements(TreeNode* root)</code> 用受污染的二叉树初始化对象,你需要先把它还原。</li>
|
||
<li><code>bool find(int target)</code> 判断目标值 <code>target</code> 是否存在于还原后的二叉树中并返回结果。</li>
|
||
</ul>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>示例 1:</strong></p>
|
||
|
||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/16/untitled-diagram-4-1.jpg" style="height: 119px; width: 320px;"></strong></p>
|
||
|
||
<pre><strong>输入:</strong>
|
||
["FindElements","find","find"]
|
||
[[[-1,null,-1]],[1],[2]]
|
||
<strong>输出:</strong>
|
||
[null,false,true]
|
||
<strong>解释:</strong>
|
||
FindElements findElements = new FindElements([-1,null,-1]);
|
||
findElements.find(1); // return False
|
||
findElements.find(2); // return True </pre>
|
||
|
||
<p><strong>示例 2:</strong></p>
|
||
|
||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/16/untitled-diagram-4.jpg" style="height: 198px; width: 400px;"></strong></p>
|
||
|
||
<pre><strong>输入:</strong>
|
||
["FindElements","find","find","find"]
|
||
[[[-1,-1,-1,-1,-1]],[1],[3],[5]]
|
||
<strong>输出:</strong>
|
||
[null,true,true,false]
|
||
<strong>解释:</strong>
|
||
FindElements findElements = new FindElements([-1,-1,-1,-1,-1]);
|
||
findElements.find(1); // return True
|
||
findElements.find(3); // return True
|
||
findElements.find(5); // return False</pre>
|
||
|
||
<p><strong>示例 3:</strong></p>
|
||
|
||
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2019/11/16/untitled-diagram-4-1-1.jpg" style="height: 274px; width: 306px;"></strong></p>
|
||
|
||
<pre><strong>输入:</strong>
|
||
["FindElements","find","find","find","find"]
|
||
[[[-1,null,-1,-1,null,-1]],[2],[3],[4],[5]]
|
||
<strong>输出:</strong>
|
||
[null,true,false,false,true]
|
||
<strong>解释:</strong>
|
||
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
|
||
</pre>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>提示:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>TreeNode.val == -1</code></li>
|
||
<li>二叉树的高度不超过 <code>20</code></li>
|
||
<li>节点的总数在 <code>[1, 10^4]</code> 之间</li>
|
||
<li>调用 <code>find()</code> 的总次数在 <code>[1, 10^4]</code> 之间</li>
|
||
<li><code>0 <= target <= 10^6</code></li>
|
||
</ul>
|