-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagonal_traversal_binary_tree.cpp
More file actions
257 lines (223 loc) · 6.17 KB
/
Diagonal_traversal_binary_tree.cpp
File metadata and controls
257 lines (223 loc) · 6.17 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
// Diagonal Tree Traversal
// link - https://www.geeksforgeeks.org/problems/diagonal-traversal-of-binary-tree/1
/*
Given a Binary Tree, print the diagonal traversal of the binary tree.
Consider lines of slope -1 passing between nodes. Given a Binary Tree, print all diagonal elements in a binary tree belonging to same line.
Example 1:
Input :
8
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13
Output : 8 10 14 3 6 7 13 1 4
Explanation: Diagonal traversal of the binary tree is:
8 10 14 3 6 7 13 1 4
Example 2:
Input :
1
/ \
4 2
/ \ \
8 5 3
/ \ /
9 7 6
Output : 1 2 3 4 5 7 6 8 9
Explanation: Diagonal traversal of the binary tree is:
1 2 3 4 5 7 6 8 9
Constraints:
1 <= Number of nodes <= 105
1 <= Data of a node <= 105
*/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
// Definition for a binary tree node.
struct Node
{
int data;
Node *left, *right;
Node(int val)
{
data = val;
left = NULL;
right = NULL;
}
};
// BFS Solution using Queue
// Time Complexity: O(n) - where n is the number of nodes
// Space Complexity: O(w) - where w is the maximum width of the tree (queue size)
class Solution
{
public:
vector<int> diagonal(Node *root)
{
if (!root)
return {};
vector<int> ans;
queue<Node *> q;
q.push(root);
while (!q.empty())
{
Node *temp = q.front();
q.pop();
while (temp)
{
ans.push_back(temp->data);
if (temp->left)
q.push(temp->left);
temp = temp->right;
}
}
return ans;
}
};
// Helper function to create a binary tree from vector
Node *createTree(vector<int> &values, int index = 0)
{
if (index >= values.size() || values[index] == -1)
{
return nullptr;
}
Node *root = new Node(values[index]);
root->left = createTree(values, 2 * index + 1);
root->right = createTree(values, 2 * index + 2);
return root;
}
// Helper function to print tree (inorder traversal)
void printTree(Node *root)
{
if (!root)
return;
printTree(root->left);
cout << root->data << " ";
printTree(root->right);
}
// Helper function to print vector
void printVector(vector<int> &vec)
{
cout << "[";
for (int i = 0; i < vec.size(); i++)
{
cout << vec[i];
if (i < vec.size() - 1)
cout << " ";
}
cout << "]";
}
int main()
{
cout << "=== Diagonal Traversal of Binary Tree ===" << endl
<< endl;
// Test Case 1: Example 1 from problem
cout << "Test Case 1:" << endl;
cout << "Tree structure:" << endl;
cout << " 8" << endl;
cout << " / \\" << endl;
cout << " 3 10" << endl;
cout << " / \\ \\" << endl;
cout << " 1 6 14" << endl;
cout << " / \\ /" << endl;
cout << " 4 7 13" << endl;
vector<int> values1 = {8, 3, 10, 1, 6, -1, 14, -1, -1, 4, 7, -1, -1, 13, -1};
Node *root1 = createTree(values1);
cout << "Tree (inorder): ";
printTree(root1);
cout << endl;
Solution sol1;
vector<int> result1 = sol1.diagonal(root1);
cout << "Diagonal Traversal: ";
printVector(result1);
cout << endl;
cout << "Expected: [8 10 14 3 6 7 13 1 4]" << endl
<< endl;
// Test Case 2: Example 2 from problem
cout << "Test Case 2:" << endl;
cout << "Tree structure:" << endl;
cout << " 1" << endl;
cout << " / \\" << endl;
cout << " 4 2" << endl;
cout << " / \\ \\" << endl;
cout << " 8 5 3" << endl;
cout << " / \\ /" << endl;
cout << " 9 7 6" << endl;
vector<int> values2 = {1, 4, 2, 8, 5, -1, 3, -1, -1, 9, 7, -1, -1, 6, -1};
Node *root2 = createTree(values2);
cout << "Tree (inorder): ";
printTree(root2);
cout << endl;
Solution sol2;
vector<int> result2 = sol2.diagonal(root2);
cout << "Diagonal Traversal: ";
printVector(result2);
cout << endl;
cout << "Expected: [1 2 3 4 5 7 6 8 9]" << endl
<< endl;
// Test Case 3: Simple tree
cout << "Test Case 3:" << endl;
vector<int> values3 = {1, 2, 3, 4, 5, 6, 7};
Node *root3 = createTree(values3);
cout << "Tree (inorder): ";
printTree(root3);
cout << endl;
Solution sol3;
vector<int> result3 = sol3.diagonal(root3);
cout << "Diagonal Traversal: ";
printVector(result3);
cout << endl;
cout << "Expected: [1 3 7 2 5 6 4]" << endl
<< endl;
// Test Case 4: Single node
cout << "Test Case 4:" << endl;
vector<int> values4 = {5};
Node *root4 = createTree(values4);
cout << "Tree (inorder): ";
printTree(root4);
cout << endl;
Solution sol4;
vector<int> result4 = sol4.diagonal(root4);
cout << "Diagonal Traversal: ";
printVector(result4);
cout << endl;
cout << "Expected: [5]" << endl
<< endl;
// Test Case 5: Left skewed tree
cout << "Test Case 5:" << endl;
vector<int> values5 = {1, 2, -1, 3, -1, -1, -1, 4};
Node *root5 = createTree(values5);
cout << "Tree (inorder): ";
printTree(root5);
cout << endl;
Solution sol5;
vector<int> result5 = sol5.diagonal(root5);
cout << "Diagonal Traversal: ";
printVector(result5);
cout << endl;
cout << "Expected: [1 2 3 4]" << endl
<< endl;
// Test Case 6: Right skewed tree
cout << "Test Case 6:" << endl;
vector<int> values6 = {1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, 4};
Node *root6 = createTree(values6);
cout << "Tree (inorder): ";
printTree(root6);
cout << endl;
Solution sol6;
vector<int> result6 = sol6.diagonal(root6);
cout << "Diagonal Traversal: ";
printVector(result6);
cout << endl;
cout << "Expected: [1 2 3 4]" << endl
<< endl;
cout << "=== Complexity Analysis ===" << endl;
cout << "BFS Solution:" << endl;
cout << "- Time Complexity: O(n) - visit each node once" << endl;
cout << "- Space Complexity: O(w) - queue size at maximum width" << endl;
cout << "- Algorithm: Level order traversal with diagonal grouping" << endl;
cout << "- Key Insight: Process all nodes in current diagonal before moving to next" << endl;
cout << "- Strategy: Use queue to store left children, traverse right children immediately" << endl;
return 0;
}