-
Notifications
You must be signed in to change notification settings - Fork 13
/
Tree_ConnectNodesAtSameLevel.java
211 lines (162 loc) · 5.49 KB
/
Tree_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
// https://practice.geeksforgeeks.org/problems/connect-nodes-at-same-level/1/
// https://practice.geeksforgeeks.org/editorial.php?pid=700184
// https://www.geeksforgeeks.org/connect-nodes-at-same-level-with-o1-extra-space/
public class Tree_ConnectNodesAtSameLevel {
// with no extra space, time : O(n) , space : O(n)
/*private static void connectNodesAtSameLevel(TreeNode root) {
if(root == null) return;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(true) {
int nodeCount = queue.size();
if(nodeCount == 0) break;
while(nodeCount > 0) {
TreeNode currNode = queue.poll();
if(nodeCount != 1) currNode.nextRight = queue.peek();
System.out.println(currNode.data + " " + (currNode.nextRight != null ? currNode.nextRight.data : "N"));
if(currNode.left != null) queue.add(currNode.left);
if(currNode.right != null) queue.add(currNode.right);
nodeCount--;
}
}
}*/
//https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
// below solution is only for PERFECT BINARY TREE
public TreeNode connectNodesAtSameLevelForPerfectBinaryTree(TreeNode root) {
if(root == null) return null;
connectRightNextNode(root.left, root.right);
return root;
}
private void connectRightNextNode(TreeNode node1, TreeNode node2) {
if(node1 == null) return;
node1.nextRight = node2;
connectRightNextNode(node1.left, node1.right);
connectRightNextNode(node2.left, node2.right);
connectRightNextNode(node1.right, node2.left);
}
// https://www.geeksforgeeks.org/connect-nodes-at-same-level-with-o1-extra-space/
// with no extra space, time : O(n) , space : O(1)
public static void connectNodesAtSameLevel(TreeNode root){
if(root == null) return;
root.nextRight = null;
TreeNode levelStart = root; //we have to keep track of first node of every level
while(levelStart != null) {
TreeNode node = levelStart;
while(node != null) {
if(node.left != null) {
if(node.right != null)
node.left.nextRight = node.right;
else
node.left.nextRight = getNextNodeFirstChild(node);
}
if(node.right != null)
node.right.nextRight = getNextNodeFirstChild(node);
node = node.nextRight;
}
if(levelStart.left != null) levelStart = levelStart.left;
else if(levelStart.right != null) levelStart = levelStart.right;
else levelStart = getNextNodeFirstChild(levelStart);
}
}
private static TreeNode getNextNodeFirstChild(TreeNode root) {
if(root == null) return null;
root = root.nextRight;
while(root != null) {
if(root.left != null) return root.left;
if(root.right != null) return root.right;
root = root.nextRight;
}
return null;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-->0) {
String ip = br.readLine().strip();
TreeNode root = buildTree(ip);
connectNodesAtSameLevel(root);
//printLevelOrder(root);
//System.out.println();
printSpecial(root);
System.out.println();
printInOrder(root);
System.out.println();
}
}
private static void printSpecial(TreeNode root) {
if (root == null)
return;
TreeNode next_root=null;
while (root != null)
{
System.out.print(root.data+" ");
if( root.left!=null && next_root==null )
next_root = root.left;
else if( root.right!=null && next_root==null )
next_root = root.right;
root = root.nextRight;
}
printSpecial(next_root);
}
static void printLevelOrder(TreeNode root) {
if(root == null) return;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
while(queue.size() != 0) {
TreeNode currNode = queue.poll();
System.out.print(currNode.data + " ");
if(currNode.left != null) {
queue.add(currNode.left);
}
if(currNode.right != null) {
queue.add(currNode.right);
}
}
}
static void printInOrder(TreeNode root) {
if(root == null) return;
printInOrder(root.left);
System.out.print(root.data + " ");
printInOrder(root.right);
}
static TreeNode buildTree(String str) {
if(str.length() == 0 || str.charAt(0) == 'N') return null;
String ip[] = str.split("\\s+");
TreeNode root = new TreeNode(Integer.parseInt(ip[0]));
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int i = 1;
while(queue.size() != 0 && i < ip.length) {
TreeNode currTreeNode = queue.poll();
String currVal = ip[i];
if(!currVal.equals("N")) {
currTreeNode.left = new TreeNode(Integer.parseInt(currVal));
queue.add(currTreeNode.left);
}
i++;
if(i >= ip.length) break;
currVal = ip[i];
if(!currVal.equals("N")) {
currTreeNode.right = new TreeNode(Integer.parseInt(currVal));
queue.add(currTreeNode.right);
}
i++;
}
return root;
}
static class TreeNode {
int data;
TreeNode left;
TreeNode right;
TreeNode nextRight;
TreeNode(int data) {
this.data = data;
this.left = this.right = this.nextRight = null;
}
}
}