-
Notifications
You must be signed in to change notification settings - Fork 0
/
Populating_Next_Right_Pointers_in_Each_Node.java
82 lines (78 loc) · 2.36 KB
/
Populating_Next_Right_Pointers_in_Each_Node.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
package com.leet_code;
import org.w3c.dom.ls.LSOutput;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Populating_Next_Right_Pointers_in_Each_Node {
public static void main(String[] args) {
Node root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.left = new Node(6);
root.right.right = new Node(7);
System.out.println(connect(root));
}
public static class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
}
public static Node connect(Node root) {// this is your chutiya giri
List <List<Integer>> result=new ArrayList<>();
if (root == null) {
return null;
}
Queue<Node> queue=new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size=queue.size();
List<Node> arr=new ArrayList<>(size);
for (int i=0;i<size;i++) {
Node node=queue.poll();
arr.add(node);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
for (int i=0;i<arr.size()-1;i++) {
arr.get(i).next=arr.get(i+1);
}
}
return root;
}
public static Node connect2(Node root) {// triky to understand please debug it...
if (root == null) {
return root;
}
Node leftmost=root;
while (leftmost.left!=null){
Node current=leftmost;
while (current!=null){
current.left.next=current.right;
if (current.next != null) {
current.right.next=current.next.left;
}
current = current.next;
}
leftmost=leftmost.left;
}
return root;
}
}