-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFindLeavesOfBinaryTree.java
More file actions
108 lines (92 loc) · 2.94 KB
/
FindLeavesOfBinaryTree.java
File metadata and controls
108 lines (92 loc) · 2.94 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
104
105
106
107
108
/*
Given a binary tree, find all leaves and then remove those leaves. Then repeat
the previous steps until the tree is empty.
Example:
Given binary tree
1
/ \
2 3
/ \
4 5
Returns [4, 5, 3], [2], [1].
Explanation:
1. Remove the leaves [4, 5, 3] from the tree
1
/
2
2. Remove the leaf [2] from the tree
1
3. Remove the leaf [1] from the tree
[]
Returns [4, 5, 3], [2], [1].
*/
import java.util.*;
public class FindLeavesOfBinaryTree {
private static int disToLeaf(TreeNode node, List<List<Integer>> disNodes) {
int curDis = 0;
if (node.left != null && node.right != null) {
curDis = Math.max(disToLeaf(node.left, disNodes),
disToLeaf(node.right, disNodes)) + 1;
} else if (node.left != null) {
curDis = disToLeaf(node.left, disNodes) + 1;
} else if (node.right != null) {
curDis = disToLeaf(node.right, disNodes) + 1;
}
List<Integer> curDisNodes = null;
if (curDis >= disNodes.size()) {
curDisNodes = new ArrayList<Integer>();
disNodes.add(curDisNodes);
} else {
curDisNodes = disNodes.get(curDis);
}
curDisNodes.add(node.val);
return curDis;
}
public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
if (root == null) {
return result;
}
disToLeaf(root, result);
return result;
}
//Best implentation:
//The order must be correct since two nodes of same height must belong
//to two different branches of a certain subtree, and when we do postorder
//traversal on that subtree, left one is always visited before the right one.
private int getHeightAndLeaves(TreeNode root, List<List<Integer>> result) {
if (root == null) {
return -1;
}
int leftHeight = getHeightAndLeaves(root.left, result);
int rightHeight = getHeightAndLeaves(root.right, result);
int height = Math.max(leftHeight, rightHeight) + 1;
if (result.size() <= height) {
result.add(new ArrayList<>());
}
result.get(height).add(root.val);
return height;
}
public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> result = new ArrayList<>();
getHeightAndLeaves(root, result);
return result;
}
//We can also solve it iteratively, but would require a map of node to height.
public static void main(String[] args) {
Integer[][] tests = new Integer[][]{
{1, 2, 3, 4, 5}, //Result = [[4, 5, 3], [2], [1]]
{1, 2, 3, null, 4, null, 6, null, 5, 7, 8, null, null, null, null, 9},
//[[5, 7, 9], [4, 8], [2, 6], [3], [1]]
{1},
{}
};
FindLeavesOfBinaryTree solution = new FindLeavesOfBinaryTree();
for (Integer[] test : tests) {
TreeNode root = TreeNode.createFromArray(test);
TreeNode.printLevels(root);
List<List<Integer>> disNodes = solution.findLeaves(root);
System.out.println(disNodes);
}
}
}