Skip to content

Commit 3cb25e3

Browse files
add 1261
1 parent 010e366 commit 3cb25e3

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ _If you like this project, please leave me a star._ ★
3737
|1271|[Hexspeak](https://leetcode.com/problems/hexspeak/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1271.java) | |Easy||
3838
|1267|[Count Servers that Communicate](https://leetcode.com/problems/count-servers-that-communicate/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1267.java) | |Medium||
3939
|1266|[Minimum Time Visiting All Points](https://leetcode.com/problems/minimum-time-visiting-all-points/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1266.java) | |Easy||
40+
|1261|[Find Elements in a Contaminated Binary Tree](https://leetcode.com/problems/find-elements-in-a-contaminated-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1261.java) ||Medium|Tree, HashTable|
4041
|1260|[Shift 2D Grid](https://leetcode.com/problems/shift-2d-grid/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1260.java) | [:tv:](https://www.youtube.com/watch?v=9hBcARSiU0s)|Easy||
4142
|1252|[Cells with Odd Values in a Matrix](https://leetcode.com/problems/cells-with-odd-values-in-a-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1252.java) | |Easy||
4243
|1237|[Find Positive Integer Solution for a Given Equation](https://leetcode.com/problems/find-positive-integer-solution-for-a-given-equation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1237.java) | |Easy||
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
/**
6+
* 1261. Find Elements in a Contaminated Binary Tree
7+
*
8+
* Given a binary tree with the following rules:
9+
* root.val == 0
10+
* If treeNode.val == x and treeNode.left != null, then treeNode.left.val == 2 * x + 1
11+
* If treeNode.val == x and treeNode.right != null, then treeNode.right.val == 2 * x + 2
12+
* Now the binary tree is contaminated, which means all treeNode.val have been changed to -1.
13+
*
14+
* You need to first recover the binary tree and then implement the FindElements class:
15+
* FindElements(TreeNode* root) Initializes the object with a contamined binary tree, you need to recover it first.
16+
* bool find(int target) Return if the target value exists in the recovered binary tree.
17+
*
18+
* Example 1:
19+
* Input
20+
* ["FindElements","find","find"]
21+
* [[[-1,null,-1]],[1],[2]]
22+
* Output
23+
* [null,false,true]
24+
* Explanation
25+
* FindElements findElements = new FindElements([-1,null,-1]);
26+
* findElements.find(1); // return False
27+
* findElements.find(2); // return True
28+
*
29+
* Example 2:
30+
* Input
31+
* ["FindElements","find","find","find"]
32+
* [[[-1,-1,-1,-1,-1]],[1],[3],[5]]
33+
* Output
34+
* [null,true,true,false]
35+
* Explanation
36+
* FindElements findElements = new FindElements([-1,-1,-1,-1,-1]);
37+
* findElements.find(1); // return True
38+
* findElements.find(3); // return True
39+
* findElements.find(5); // return False
40+
*
41+
* Example 3:
42+
* Input
43+
* ["FindElements","find","find","find","find"]
44+
* [[[-1,null,-1,-1,null,-1]],[2],[3],[4],[5]]
45+
* Output
46+
* [null,true,false,false,true]
47+
* Explanation
48+
* FindElements findElements = new FindElements([-1,null,-1,-1,null,-1]);
49+
* findElements.find(2); // return True
50+
* findElements.find(3); // return False
51+
* findElements.find(4); // return False
52+
* findElements.find(5); // return True
53+
*
54+
* Constraints:
55+
* TreeNode.val == -1
56+
* The height of the binary tree is less than or equal to 20
57+
* The total number of nodes is between [1, 10^4]
58+
* Total calls of find() is between [1, 10^4]
59+
* 0 <= target <= 10^6
60+
* */
61+
public class _1261 {
62+
public static class Solution1 {
63+
class FindElements {
64+
65+
TreeNode recoveredRoot;
66+
67+
public FindElements(TreeNode root) {
68+
recoveredRoot = new TreeNode(0);
69+
recoveredRoot = recoverTree(root, recoveredRoot);
70+
}
71+
72+
private TreeNode recoverTree(TreeNode root, TreeNode recoveredRoot) {
73+
if (root == null) {
74+
return recoveredRoot;
75+
}
76+
if (root.left != null) {
77+
recoveredRoot.left = new TreeNode(recoveredRoot.val * 2 + 1);
78+
}
79+
if (root.right != null) {
80+
recoveredRoot.right = new TreeNode(recoveredRoot.val * 2 + 2);
81+
}
82+
recoverTree(root.left, recoveredRoot.left);
83+
recoverTree(root.right, recoveredRoot.right);
84+
return recoveredRoot;
85+
}
86+
87+
public boolean find(int target) {
88+
return find(recoveredRoot, target);
89+
}
90+
91+
private boolean find(TreeNode root, int target) {
92+
if (root == null) {
93+
return false;
94+
} else if (root.val == target) {
95+
return true;
96+
}
97+
return find(root.left, target) || find(root.right, target);
98+
}
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)