Skip to content

Commit c63d532

Browse files
add 1261
1 parent 3010e60 commit c63d532

File tree

7 files changed

+151
-5
lines changed

7 files changed

+151
-5
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -565,4 +565,5 @@ LeetCode
565565
|1257|[Smallest Common Region](https://leetcode.com/contest/biweekly-contest-13/problems/smallest-common-region/)|c|[c++](./src/1257-Smallest-Common-Region/1257.cpp)|[python](./src/1257-Smallest-Common-Region/1257.py)|[go](./src/1257-Smallest-Common-Region/1257.go)|[js](./src/1257-Smallest-Common-Region/1257.js)|[java](./src/1257-Smallest-Common-Region/1257.java)|Medium|
566566
|1258|[Synonymous Sentences](https://leetcode.com/contest/biweekly-contest-13/problems/synonymous-sentences/)|c|[c++](./src/1258-Synonymous-Sentences/1258.cpp)|[python](./src/1258-Synonymous-Sentences/1258.py)|[go](./src/1258-Synonymous-Sentences/1258.go)|[js](./src/1258-Synonymous-Sentences/1258.js)|[java](./src/1258-Synonymous-Sentences/1258.java)|Medium|
567567
|1259|[Handshakes That Don't Cross](https://leetcode.com/contest/biweekly-contest-13/problems/handshakes-that-dont-cross/)|c|[c++](./src/1259-Handshakes-That-Don't-Cross/1259.cpp)|[python](./src/1259-Handshakes-That-Don't-Cross/1259.py)|[go](./src/1259-Handshakes-That-Don't-Cross/1259.go)||[java](./src/1259-Handshakes-That-Don't-Cross/1259.java)|Hard|
568-
|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|c|[c++](./src/1260-Shift-2D-Grid/1260.cpp)|[python](./src/1260-Shift-2D-Grid/1260.py)|[go](./src/1260-Shift-2D-Grid/1260.go)|[js](./src/1260-Shift-2D-Grid/1260.js)|[java](./src/1260-Shift-2D-Grid/1260.java)|Easy|
568+
|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|c|[c++](./src/1260-Shift-2D-Grid/1260.cpp)|[python](./src/1260-Shift-2D-Grid/1260.py)|[go](./src/1260-Shift-2D-Grid/1260.go)|[js](./src/1260-Shift-2D-Grid/1260.js)|[java](./src/1260-Shift-2D-Grid/1260.java)|Easy|
569+
|1261|[Find Elements in a Contaminated Binary Tree](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/)|c|[c++](./src/1261-Find-Elements-in-a-Contaminated-Binary-Tree/1261.cpp)|[python](./src/1261-Find-Elements-in-a-Contaminated-Binary-Tree/1261.py)|[go](./src/1261-Find-Elements-in-a-Contaminated-Binary-Tree/1261.go)|[js](./src/1261-Find-Elements-in-a-Contaminated-Binary-Tree/1261.js)|[java](./src/1261-Find-Elements-in-a-Contaminated-Binary-Tree/1261.java)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class FindElements
2+
{
3+
public:
4+
FindElements(TreeNode* root)
5+
{
6+
root->val = 0;
7+
data.insert(0);
8+
dfs(root);
9+
}
10+
11+
bool find(int target)
12+
{
13+
return data.count(target);
14+
}
15+
16+
private:
17+
unordered_set<int> data;
18+
19+
void dfs(TreeNode* root)
20+
{
21+
if (root->left)
22+
{
23+
root->left->val = 2 * root->val + 1;
24+
data.insert(root->left->val);
25+
dfs(root->left);
26+
}
27+
if (root->right)
28+
{
29+
root->right->val = 2 * root->val + 2;
30+
data.insert(root->right->val);
31+
dfs(root->right);
32+
}
33+
}
34+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
type FindElements struct {
2+
data map[int]int
3+
}
4+
5+
6+
func Constructor(root *TreeNode) FindElements {
7+
res := FindElements{data:make(map[int]int)}
8+
root.Val, res.data[0] = 0, 1
9+
dfs(&res, root)
10+
return res
11+
}
12+
13+
func dfs(this *FindElements, root* TreeNode) {
14+
if root.Left != nil {
15+
root.Left.Val = 2 * root.Val + 1
16+
this.data[root.Left.Val] = 1
17+
dfs(this, root.Left)
18+
}
19+
if root.Right != nil {
20+
root.Right.Val = 2 * root.Val + 2
21+
this.data[root.Right.Val] = 1
22+
dfs(this, root.Right)
23+
}
24+
}
25+
26+
27+
func (this *FindElements) Find(target int) bool {
28+
if _, ok := this.data[target]; ok {
29+
return true
30+
}
31+
return false
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class FindElements {
2+
public FindElements(TreeNode root) {
3+
root.val = 0;
4+
data.add(0);
5+
dfs(root);
6+
}
7+
8+
private Set<Integer> data = new HashSet();
9+
10+
private void dfs(TreeNode root) {
11+
if (root.left != null) {
12+
root.left.val = root.val*2 + 1;
13+
data.add(root.left.val);
14+
dfs(root.left);
15+
}
16+
if (root.right != null) {
17+
root.right.val = root.val*2 + 2;
18+
data.add(root.right.val);
19+
dfs(root.right);
20+
}
21+
}
22+
23+
public boolean find(int target) {
24+
return data.contains(target);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {TreeNode} root
3+
*/
4+
var FindElements = function(root) {
5+
this.data = new Set();
6+
root.val = 0;
7+
this.data.add(0);
8+
9+
let dfs = node => {
10+
if (node.left) {
11+
node.left.val = node.val * 2 + 1;
12+
this.data.add(node.left.val);
13+
dfs(node.left);
14+
}
15+
if (node.right) {
16+
node.right.val = node.val * 2 + 2;
17+
this.data.add(node.right.val);
18+
dfs(node.right);
19+
}
20+
}
21+
dfs(root);
22+
};
23+
24+
/**
25+
* @param {number} target
26+
* @return {boolean}
27+
*/
28+
FindElements.prototype.find = function(target) {
29+
return this.data.has(target);
30+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class FindElements:
2+
def __init__(self, root: TreeNode):
3+
root.val = 0
4+
def dfs(node):
5+
if node.left:
6+
node.left.val = 2*node.val + 1
7+
dfs(node.left)
8+
if node.right:
9+
node.right.val = 2*node.val + 2
10+
dfs(node.right)
11+
dfs(root)
12+
self.root = root
13+
14+
def find(self, target: int) -> bool:
15+
cur = self.root
16+
for i in bin(target + 1)[3:]:
17+
if not cur:
18+
return False
19+
if i == '1':
20+
cur = cur.right
21+
else:
22+
cur = cur.left
23+
return cur != None

src/addProb.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import os, bisect
33

44
# 题目名称
5-
name = "Shift 2D Grid"
6-
ID = 1260
7-
url = "https://leetcode.com/problems/shift-2d-grid/"
8-
difficult = "Easy"
5+
name = "Find Elements in a Contaminated Binary Tree"
6+
ID = 1261
7+
url = "https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/"
8+
difficult = "Medium"
99
prog = ['c', 'cpp', 'py', 'go', 'js', 'java']
1010

1111

0 commit comments

Comments
 (0)