-
Notifications
You must be signed in to change notification settings - Fork 10
/
Day-107.cpp
42 lines (42 loc) · 1.16 KB
/
Day-107.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
/**
* Definition for a binary tree node.
* 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) {
queue<TreeNode*>q;
q.push(root);
TreeNode *tempNode = nullptr;
q.push(tempNode);
vector<vector<int>>res;
if(root==nullptr){return res;}
vector<int>ans;
while(q.size()){
TreeNode*t = q.front(); q.pop();
if(t == nullptr){
if(q.size())
q.push(tempNode);
if(ans.size())
res.push_back(ans);
ans.clear();
continue;
}
ans.push_back(t->val);
if(t->left != nullptr){
q.push(t->left);
}
if(t->right != nullptr){
q.push(t->right);
}
}
return res;
}
};