Skip to content

Commit 34c3cb5

Browse files
committed
Sync LeetCode submission Runtime - 7 ms (65.78%), Memory - 22.2 MB (66.31%)
1 parent 486f283 commit 34c3cb5

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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>&nbsp;</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+
[&quot;FindElements&quot;,&quot;find&quot;,&quot;find&quot;]
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+
[&quot;FindElements&quot;,&quot;find&quot;,&quot;find&quot;,&quot;find&quot;]
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+
[&quot;FindElements&quot;,&quot;find&quot;,&quot;find&quot;,&quot;find&quot;,&quot;find&quot;]
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>&nbsp;</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 &lt;= target &lt;= 10<sup>6</sup></code></li>
75+
</ul>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Approach 1: DFS
2+
3+
# Time:
4+
# constructor: O(n)
5+
# find(): O(1)
6+
7+
# Space: O(n)
8+
9+
# Definition for a binary tree node.
10+
# class TreeNode:
11+
# def __init__(self, val=0, left=None, right=None):
12+
# self.val = val
13+
# self.left = left
14+
# self.right = right
15+
16+
class FindElements:
17+
18+
def __init__(self, root: Optional[TreeNode]):
19+
self.seen = set()
20+
self.dfs(root, 0)
21+
22+
def find(self, target: int) -> bool:
23+
return target in self.seen
24+
25+
def dfs(self, current_node, current_value):
26+
if current_node is None:
27+
return
28+
29+
self.seen.add(current_value)
30+
self.dfs(current_node.left, current_value * 2 + 1)
31+
self.dfs(current_node.right, current_value * 2 + 2)
32+
33+
34+
35+
# Your FindElements object will be instantiated and called as such:
36+
# obj = FindElements(root)
37+
# param_1 = obj.find(target)

0 commit comments

Comments
 (0)