-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAllNodesDistanceKInBinaryTree.java
103 lines (84 loc) · 2.94 KB
/
AllNodesDistanceKInBinaryTree.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package problems.leetcode;
import java.util.ArrayList;
import java.util.List;
/**
* https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/
*/
public class AllNodesDistanceKInBinaryTree {
private static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
private List<Integer> result = new ArrayList<>();
public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
find(root, target.val, K);
return result;
}
private int find(TreeNode node, int targetVal, int targetDistance) {
if (node == null) {
return 0;
}
if (node.val == targetVal) {
if (targetDistance == 0) {
result.add(node.val);
return -1;
}
traverseUntilDepth(node.left, 1, targetDistance);
traverseUntilDepth(node.right, 1, targetDistance);
return 1;
}
int distance = find(node.left, targetVal, targetDistance);
if (distance > 0) {
if (distance == targetDistance) {
result.add(node.val);
}
traverseUntilDepth(node.right, 1, targetDistance - distance);
} else {
distance = find(node.right, targetVal, targetDistance);
if (distance > 0) {
if (distance == targetDistance) {
result.add(node.val);
}
traverseUntilDepth(node.left, 1, targetDistance - distance);
}
}
return distance > 0 ? distance + 1 : 0;
}
private void traverseUntilDepth(TreeNode node, int depth, int targetDepth) {
if (node == null) {
return;
} else if (depth == targetDepth) {
result.add(node.val);
return;
}
traverseUntilDepth(node.left, depth + 1, targetDepth);
traverseUntilDepth(node.right, depth + 1, targetDepth);
}
public static void main(String[] args) {
TreeNode target = new TreeNode(5);
TreeNode root2 = new TreeNode(15);
TreeNode root = new TreeNode(3);
root.left = target;
root.left.left = new TreeNode(6);
root.left.right = new TreeNode(2);
root.left.right.left = new TreeNode(7);
root.left.right.right = new TreeNode(4);
root.right = new TreeNode(1);
root.right.left = new TreeNode(0);
root.right.right = new TreeNode(8);
root2.right = root;
root2.left = new TreeNode(20);
// TreeNode root = new TreeNode(0);
// root.left = new TreeNode(1);
// root.left.right = new TreeNode(2);
// root.left.right.right = new TreeNode(3);
// root.left.right.right.right = new TreeNode(4);
int K = 1;
List<Integer> result = new AllNodesDistanceKInBinaryTree().distanceK(root, target, K);
System.out.println(result);
}
}