-
Notifications
You must be signed in to change notification settings - Fork 396
/
Copy pathpopulatingNextRightPointersInNodeII.java
94 lines (79 loc) · 2.8 KB
/
populatingNextRightPointersInNodeII.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
Given a binary tree. THIS IS THE DIFFERENCE
// struct Node {
// int val;
// Node *left;
// Node *right;
// Node *next;
// }
// Populate each next pointer to point to its next right node.
// If there is no next right node, the next pointer should be set to NULL.
// Initially, all next pointers are set to NULL.
LEVEL ORDER BFS, same as popNextrightI problem
TC: O(N) to process each node once
SC: O(N) This is a binary tree which means the last level could contain N/2 nodes.
The space complexity for breadth first traversal is the maximum space occupied and the space
occupied by the queue is dependent upon the maximum number of nodes in particular level. So,
in this case, the space complexity would be O(N).
class Solution {
public Node connect(Node root) {
if(root == null) return null;
Queue<Node> q = new LinkedList<>();
q.add(root);
while(!q.isEmpty()){
int size = q.size();
for(int i=0; i<size; i++){
Node curr = q.poll();
if(i == size-1){
curr.next = null;
} else{
curr.next = q.peek();
}
if(curr.left!=null){
q.add(curr.left);
}
if(curr.right!=null){
q.add(curr.right);
}
}
}
return root;
}
}
SPACE EFFICIENT CONSTANT SPACE Solution
BASICALLY NEED TO MAKE USE OF A PREVIOUS NODE!!
WE CAN GET CONSTANT SPACE BECAUSE, the next pointers allow us to perform
level order BFS by use of next pointers (which act as a sort of queue)
//populate pointers on next level by the two following cases
//1)
//2)
TC: O(N)
SC: O(1)
public class Solution {
public void connect(TreeLinkNode root) {
TreeLinkNode head=root;//The LEFTMOST node in the lower level
TreeLinkNode prev=null;//The LEADING NODE node in the lower level, that iterates through it
TreeLinkNode curr=null;//The current node in the upper level
while (head!=null){
curr=head; //reset the curr to be the leftMost node on the lower level, and iterate again
prev=null; //reset these to null
head=null; //reset these to null
while (curr!=null){
if (curr.left!=null){
if (prev!=null)
prev.next=curr.left;
else
head=curr.left;
prev=curr.left;
}
if (curr.right!=null){
if (prev!=null)
prev.next=curr.right;
else
head=curr.right;
prev=curr.right;
}
curr=curr.next;
}
}
}
}