-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Tree_Level_Order_Traversal.cpp
More file actions
92 lines (88 loc) · 3.03 KB
/
Binary_Tree_Level_Order_Traversal.cpp
File metadata and controls
92 lines (88 loc) · 3.03 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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//O(N)
//O(N)
class Solution {
public:
vector<vector<int> > levelOrder(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int>> res;
if(root==NULL)return res;
vector<vector<TreeNode*>> table(2);
int pre=0,cur=1;
table[cur].push_back(root);
vector<int> tmp;
tmp.push_back(root->val);
res.push_back(tmp);
while(true){
cur=!cur;
pre=!pre;
table[cur].clear();
vector<int> tmp;
for(int i=0;i<table[pre].size();i++)
{
if(table[pre][i]->left)
{
table[cur].push_back(table[pre][i]->left);
tmp.push_back(table[pre][i]->left->val);
}
if(table[pre][i]->right)
{
table[cur].push_back(table[pre][i]->right);
tmp.push_back(table[pre][i]->right->val);
}
}
if(table[cur].empty())break;
res.push_back(tmp);
}
return res;
}
vector<vector<int> > levelOrder(TreeNode *root) {
vector<vector<int> > res;
vector<int> row;
queue<TreeNode*> currQ, nextQ;
if (root) currQ.push(root);
while (!currQ.empty()) {
row.clear();
while (!currQ.empty()) {
TreeNode* front = currQ.front();
currQ.pop();
row.push_back(front->val);
if (front->left) nextQ.push(front->left);
if (front->right) nextQ.push(front->right);
}
swap(currQ, nextQ);
res.push_back(row);
}
return res;
}
vector<vector<int> > levelOrder(TreeNode *root) {
vector<vector<int> > res;
vector<int> row;
queue<TreeNode*> currQ;
int currNum = 1, nextNum = 0;
if (root) currQ.push(root);
while (!currQ.empty()) {
TreeNode* front = currQ.front();
currQ.pop(), currNum--;
row.push_back(front->val);
if (front->left) currQ.push(front->left), nextNum++;
if (front->right) currQ.push(front->right), nextNum++;
if (currNum == 0) {
res.push_back(row);
row.clear();
currNum = nextNum;
nextNum = 0;
}
}
return res;
}
};