-
Notifications
You must be signed in to change notification settings - Fork 1
/
47_binary-tree-level-order-traversal.cpp
132 lines (109 loc) · 3.08 KB
/
47_binary-tree-level-order-traversal.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
// DATE: 07-Aug-2023
/* PROGRAM: 47_Tree - Binary Tree Level Order Traversal
https://leetcode.com/problems/binary-tree-level-order-traversal/
Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from
left to right, level by level).
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
Example 2:
Input: root = [1]
Output: [[1]]
Example 3:
Input: root = []
Output: []
*/
// @ankitsamaddar @Aug_2023
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
};
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if (!root) {
return {};
}
vector<vector<int>> ans;
queue<TreeNode*> q;
q.push(root);
while (!q.empty()) {
int qLen = q.size();
vector<int> curLevel;
for (int i = 0; i < qLen; i++) {
TreeNode* node = q.front();
q.pop();
if (node) {
curLevel.push_back(node->val);
q.push(node->left);
q.push(node->right);
}
}
if (!curLevel.empty()) {
ans.push_back(curLevel);
}
}
return ans;
}
};
vector<int> parseInput(string input) {
vector<int> result;
int num = 0;
bool negative = false;
for (char c : input) {
if (isdigit(c)) {
num = num * 10 + (c - '0');
} else if (c == 'n') {
num = INT_MAX;
} else if (c == '-') {
negative = true;
} else if (c == ',' || c == ']') {
result.push_back(negative ? -num : num);
num = 0;
negative = false;
}
}
return result;
}
TreeNode* createTree(vector<int>& nums, int i) {
if (i >= nums.size() || nums[i] == INT_MAX) {
return NULL;
}
TreeNode* root = new TreeNode(nums[i]);
root->left = createTree(nums, 2 * i + 1);
root->right = createTree(nums, 2 * i + 2);
return root;
}
string formatOutput(vector<vector<int>>& output) {
string result = "[";
for (auto& level : output) {
result += "[";
for (int i = 0; i < level.size(); i++) {
result += to_string(level[i]);
if (i < level.size() - 1) {
result += ",";
}
}
result += "]";
}
result += "]";
return result;
}
int main() {
string input = "[3,9,20,null,null,15,7]";
vector<int> nums = parseInput(input);
TreeNode* root = createTree(nums, 0);
Solution sol;
vector<vector<int>> output = sol.levelOrder(root);
string formattedOutput = formatOutput(output);
cout << formattedOutput << endl; // expected output: [[3],[9,20],[15,7]]
return 0;
}