- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1261.java
45 lines (38 loc) · 1.46 KB
/
_1261.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
packagecom.fishercoder.solutions.secondthousand;
importcom.fishercoder.common.classes.TreeNode;
publicclass_1261 {
publicstaticclassSolution1 {
classFindElements {
TreeNoderecoveredRoot;
publicFindElements(TreeNoderoot) {
recoveredRoot = newTreeNode(0);
recoveredRoot = recoverTree(root, recoveredRoot);
}
privateTreeNoderecoverTree(TreeNoderoot, TreeNoderecoveredRoot) {
if (root == null) {
returnrecoveredRoot;
}
if (root.left != null) {
recoveredRoot.left = newTreeNode(recoveredRoot.val * 2 + 1);
}
if (root.right != null) {
recoveredRoot.right = newTreeNode(recoveredRoot.val * 2 + 2);
}
recoverTree(root.left, recoveredRoot.left);
recoverTree(root.right, recoveredRoot.right);
returnrecoveredRoot;
}
publicbooleanfind(inttarget) {
returnfind(recoveredRoot, target);
}
privatebooleanfind(TreeNoderoot, inttarget) {
if (root == null) {
returnfalse;
} elseif (root.val == target) {
returntrue;
}
returnfind(root.left, target) || find(root.right, target);
}
}
}
}