-
Notifications
You must be signed in to change notification settings - Fork 0
/
populating_next_right_pointers_in_each_node_ii.cpp
146 lines (122 loc) · 4.04 KB
/
populating_next_right_pointers_in_each_node_ii.cpp
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
// https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
// June 23, 2016
#include <iostream>
using namespace std;
struct TreeLinkNode
{
int val;
TreeLinkNode* left;
TreeLinkNode* right;
TreeLinkNode* next;
TreeLinkNode(int x): val(x), left(NULL), right(NULL), next(NULL) {}
};
class Solution {
public:
void connect(TreeLinkNode *root)
{
if (root == NULL)
{
return;
}
TreeLinkNode* prevLevelHead = root; // head of the linked list at previous level
while (prevLevelHead)
{
TreeLinkNode* curLevelHead = NULL;
TreeLinkNode* nodePrevLevel = prevLevelHead;
// iterate the nodes at previous level
TreeLinkNode* prevNodeCurLevel = NULL;
while (nodePrevLevel)
{
if (nodePrevLevel->left)
{
TreeLinkNode* curNodeCurLevel = nodePrevLevel->left;
if (curLevelHead == NULL)
{
curLevelHead = nodePrevLevel->left;
}
else
{
// prevNodeCurLevel will exist
prevNodeCurLevel->next = curNodeCurLevel;
}
prevNodeCurLevel = curNodeCurLevel;
}
if (nodePrevLevel->right)
{
TreeLinkNode* curNodeCurLevel = nodePrevLevel->right;
if (curLevelHead == NULL)
{
curLevelHead = nodePrevLevel->right;
}
else
{
// prevNodeCurLevel will exist
prevNodeCurLevel->next = curNodeCurLevel;
}
prevNodeCurLevel = curNodeCurLevel;
}
nodePrevLevel = nodePrevLevel->next;
}
// storing the current level's head as previous level's head before iterating the next level
prevLevelHead = curLevelHead;
}
}
void print_levels(TreeLinkNode* root)
{
// Outer traverse: Left side view of binary tree
// Inner traversal: Level traversal using next
TreeLinkNode* leftMostNodeCurLevel = root;
while (leftMostNodeCurLevel)
{
TreeLinkNode* leftMostNodeNextLevel = NULL;
// We will print the nodes at current level as well as find the left most for the next level
TreeLinkNode* nodeCurLevel = leftMostNodeCurLevel;
while (nodeCurLevel)
{
cout << nodeCurLevel->val << ",";
if (leftMostNodeNextLevel == NULL)
{
if (nodeCurLevel->left)
{
leftMostNodeNextLevel = nodeCurLevel->left;
}
}
if (leftMostNodeNextLevel == NULL)
{
if (nodeCurLevel->right)
{
leftMostNodeNextLevel = nodeCurLevel->right;
}
}
nodeCurLevel = nodeCurLevel->next;
}
cout << endl;
// assigning for the next iteration
leftMostNodeCurLevel = leftMostNodeNextLevel;
}
}
};
TreeLinkNode* create_tree()
{
TreeLinkNode* node1 = new TreeLinkNode(1);
TreeLinkNode* node2 = new TreeLinkNode(2);
TreeLinkNode* node3 = new TreeLinkNode(3);
TreeLinkNode* node4 = new TreeLinkNode(4);
TreeLinkNode* node5 = new TreeLinkNode(5);
TreeLinkNode* node6 = new TreeLinkNode(6);
TreeLinkNode* node7 = new TreeLinkNode(7);
node1->left = node2;
node1->right = node3;
// node2->left = node4;
node2->right = node5;
node3->right = node7;
return node1;
}
int main(int argc, char* argv[])
{
TreeLinkNode* root = create_tree();
Solution sln;
sln.connect(root);
sln.print_levels(root);
return 0;
}