forked from jsjtzyy/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC366_FindLeavesOfBinaryTree.java
More file actions
106 lines (99 loc) · 3.01 KB
/
LC366_FindLeavesOfBinaryTree.java
File metadata and controls
106 lines (99 loc) · 3.01 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
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
import java.util.*;
/*
method1: topological sort
method2: use the height of each node to detetmine when should be deleted.
*/
public class LC366_FindLeavesOfBinaryTree{
/*
------------- method1 -------------------------
*/
public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null) return res;
Node node = new Node(null, root.val);
Queue<Node> queue = new LinkedList<>();
if(root.left != null){
node.outDegree++;
traverse(node, root.left, queue);
}
if(root.right != null){
node.outDegree++;
traverse(node, root.right, queue);
}
if(node.outDegree == 0) queue.offer(node);
Node tmp = null;
List<Integer> list = new ArrayList<>();
while(!queue.isEmpty()){
Queue<Node> next = new LinkedList<>();
while(!queue.isEmpty()){
tmp = queue.poll();
list.add(tmp.val);
if(tmp.father != null && --tmp.father.outDegree == 0) next.offer(tmp.father);
}
res.add(list);
list = new ArrayList<>();
queue = next;
}
return res;
}
public void traverse(Node father, TreeNode child, Queue<Node> queue){
Node node = new Node(father, child.val);
if(child.left != null){
node.outDegree++;
traverse(node, child.left, queue);
}
if(child.right != null){
node.outDegree++;
traverse(node, child.right, queue);
}
if(node.outDegree == 0) queue.offer(node);
}
class Node implements Comparable<Node>{
int outDegree;
Node father;
int val;
public Node(Node father, int val){
this.father = father;
outDegree = 0;
this.val = val;
}
@Override
public int compareTo(Node that){
return this.outDegree - that.outDegree;
}
}
/*
------------- method2 -------------------------
*/
public List<List<Integer>> findLeaves2(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
if(root == null) return res;
Map<Integer, List<Integer>> map = new TreeMap<>(); // O(nlogn)
int height = 1 + Math.max(Helper(root.left, map), Helper(root.right, map));
List<Integer> list = null;
if(map.containsKey(height)){
list = map.get(height);
}else{
list = new ArrayList<>();
}
list.add(root.val);
map.put(height, list);
for(Integer num : map.keySet()){
res.add(map.get(num));
}
return res;
}
public int Helper(TreeNode node, Map<Integer, List<Integer>> map){
if(node == null) return 0;
int height = 1 + Math.max(Helper(node.left, map), Helper(node.right, map));
List<Integer> list = null;
if(map.containsKey(height)){
list = map.get(height);
}else{
list = new ArrayList<>();
}
list.add(node.val);
map.put(height, list);
return height;
}
}