forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectNodesAtSameLevel.java
127 lines (102 loc) · 3.6 KB
/
ConnectNodesAtSameLevel.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.rampatra.trees;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
/**
* Given a binary tree, return lists of nodes at each level. The number of lists in the output will be equal to
* the number of levels in the tree.
*
* @author rampatra
* @since 2019-04-02
*/
public class ConnectNodesAtSameLevel {
private static class TreeNode {
Integer val;
TreeNode left;
TreeNode right;
TreeNode(Integer val) {
this.val = val;
}
@Override
public String toString() {
return Integer.toString(val);
}
}
private static List<List<TreeNode>> connectNodes(TreeNode root) {
if (root == null) return null;
Queue<TreeNode> queue = new ArrayDeque<>();
List<List<TreeNode>> allNodes = new ArrayList<>();
List<TreeNode> connectedNodesAtLevel = new ArrayList<>();
queue.add(root);
queue.add(new TreeNode(null)); // we use a node with null value as a marker for each level
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node.val != null) {
connectedNodesAtLevel.add(node);
} else { // when we encounter a null in the queue, we know that a level is completed
allNodes.add(connectedNodesAtLevel);
connectedNodesAtLevel = new ArrayList<>();
if (queue.peek() != null) queue.add(new TreeNode(null));
continue;
}
if (node.left != null) queue.add(node.left);
if (node.right != null) queue.add(node.right);
}
return allNodes;
}
public static void main(String[] args) {
/*
The BST looks like:
4
/ \
2 8
/ \ / \
1 3 6 9
/
0
*/
TreeNode treeRoot = new TreeNode(4);
treeRoot.left = new TreeNode(2);
treeRoot.right = new TreeNode(8);
treeRoot.left.left = new TreeNode(1);
treeRoot.left.right = new TreeNode(3);
treeRoot.left.left.left = new TreeNode(0);
treeRoot.right.left = new TreeNode(6);
treeRoot.right.right = new TreeNode(9);
connectNodes(treeRoot).forEach(System.out::println);
System.out.println("--------------");
/*
The BST looks like:
4
/ \
2 8
\ / \
3 6 9
*/
treeRoot = new TreeNode(4);
treeRoot.left = new TreeNode(2);
treeRoot.right = new TreeNode(8);
treeRoot.left.right = new TreeNode(3);
treeRoot.right.left = new TreeNode(6);
treeRoot.right.right = new TreeNode(9);
connectNodes(treeRoot).forEach(System.out::println);
System.out.println("--------------");
/*
The BST looks like:
4
/ \
2 8
/ \ / \
1 3 6 9
*/
treeRoot = new TreeNode(4);
treeRoot.left = new TreeNode(2);
treeRoot.right = new TreeNode(8);
treeRoot.left.left = new TreeNode(1);
treeRoot.left.right = new TreeNode(3);
treeRoot.right.left = new TreeNode(6);
treeRoot.right.right = new TreeNode(9);
connectNodes(treeRoot).forEach(System.out::println);
}
}