-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution545.java
More file actions
68 lines (65 loc) · 2.13 KB
/
Copy pathSolution545.java
File metadata and controls
68 lines (65 loc) · 2.13 KB
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution545 {
public List<Integer> boundaryOfBinaryTree(TreeNode root) {
// 只有根节点的特例
if (root.left == null && root.right == null) return new ArrayList<>() {{add(root.val);}};
List<Integer> res = new ArrayList<>();
List<Integer> leftBoundary = null;
List<Integer> rightBoundary = null;
List<Integer> leafBoundary = new ArrayList<>();
res.add(root.val);
if (root.left != null) {
leftBoundary = getLeftBoundary(root);
for (int t : leftBoundary) res.add(t);
}
getLeaf(root, leafBoundary);
for (int t : leafBoundary) res.add(t);
if (root.right != null) {
rightBoundary = getRightBoundary(root);
for (int t : rightBoundary) res.add(t);
}
return res;
}
public List<Integer> getLeftBoundary(TreeNode root) {
List<Integer> res= new LinkedList<>();
while (root != null) {
res.add(root.val);
if (root.left != null) root = root.left;
else root = root.right;
}
res.remove(0);
res.remove(res.size() - 1);
return res;
}
public List<Integer> getRightBoundary(TreeNode root) {
List<Integer> res= new LinkedList<>();
while (root != null) {
res.add(0, root.val);
if (root.right != null) root = root.right;
else root = root.left;
}
res.remove(res.size() - 1);
res.remove(0);
return res;
}
public void getLeaf(TreeNode root, List<Integer> leafBoundary) {
if (root == null) return;
if (root.left == null && root.right == null) leafBoundary.add(root.val);
getLeaf(root.left, leafBoundary);
getLeaf(root.right, leafBoundary);
}
}