-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPopulateLevelPointers
More file actions
58 lines (53 loc) · 2.08 KB
/
PopulateLevelPointers
File metadata and controls
58 lines (53 loc) · 2.08 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
class ListTreeNode{
int val;
ListTreeNode left;
ListTreeNode right;
ListTreeNode next;
public ListTreeNode(int val) {
this.val = val;
this.left = null;
this.right = null;
this.next = null;
}
}
public class PopulateLevelPointers {
public ListTreeNode connect (ListTreeNode root){
if ( root == null){
return null;
}
ListTreeNode currentNode = root;// Start at the root node
while (currentNode.left != null){ // Traverse each level of the tree until there are no more levels
ListTreeNode level = currentNode;// Set the starting node of the level to the current node
while (level != null){ // Traverse each node of the level
level.left.next = level.right;// Connect the left child node to the right child node
if ( level.next != null){
level.right.next = level.next.left; // Connect the right child node to the left child node of the next node on the same level
}
level = level.next;// Move to the next node on the same level
}
currentNode = currentNode.left;// Move to the next level of the tree
}
return root;// Return the modified tree
}
public static void main(String[] args) {
ListTreeNode root = new ListTreeNode(1);
root.left = new ListTreeNode(2);
root.right = new ListTreeNode(3);
root.left.left = new ListTreeNode(4);
root.left.right = new ListTreeNode(5);
root.right.left = new ListTreeNode(6);
root.right.right = new ListTreeNode(7);
PopulateLevelPointers plp = new PopulateLevelPointers();
ListTreeNode result = plp.connect(root);
ListTreeNode current = result;
while (current != null){
ListTreeNode level = current;
while (level != null){
System.out.print(level.val + " -> ");
level = level.next;
}
System.out.println("x");
current = current.left;
}
}
}