-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathListOfDepths.java
72 lines (68 loc) · 1.59 KB
/
ListOfDepths.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
package chapter04TreesAndGraphs;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
*
* Problem: Given a binary tree, design an algorithm which creates a linked list
* of all the nodes at each depth (e.g., if you have a tree with depth D, you'll
* have D linked lists).
*
*/
public class ListOfDepths {
/**
* method 1: DFS A simple modification of pre-order traversal;
*
* Time: O(N); Extra Space: O(logN)
*/
public List<LinkedList<TreeNode>> create(TreeNode root) {
List<LinkedList<TreeNode>> res = new ArrayList<>();
helper(res, root, 0);
return res;
}
private void helper(List<LinkedList<TreeNode>> res, TreeNode node, int level) {
// base case
if (node == null) {
return;
}
LinkedList<TreeNode> list = null;
if (res.size() == level) {
list = new LinkedList<>();
res.add(list);
} else {
list = res.get(level);
}
list.add(node);
helper(res, node.left, level + 1);
helper(res, node.right, level + 1);
}
/**
* method 2: BFS
*
* Time: O(N), Extra Space: O(1)
*
*/
public List<LinkedList<TreeNode>> create2(TreeNode root) {
List<LinkedList<TreeNode>> res = new ArrayList<>();
if (root == null) {
return res;
}
LinkedList<TreeNode> cur = new LinkedList<>();
cur.add(root);
while (cur.size() > 0) {
res.add(cur);
// go to the next level
LinkedList<TreeNode> parents = cur;
cur = new LinkedList<>();
for (TreeNode parent : parents) {
if (parent.left != null) {
cur.add(parent.left);
}
if (parent.right != null) {
cur.add(parent.right);
}
}
}
return res;
}
}