Skip to content

Commit c78222d

Browse files
authored
Create 102. Binary Tree Level Order Traversal
1 parent 5b359f9 commit c78222d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> levelOrder(TreeNode* root) {
4+
5+
vector<vector<int>> res ;
6+
if(root == NULL){
7+
return res ;
8+
}
9+
10+
queue<TreeNode*>q;
11+
q.push(root);
12+
while(!q.empty())
13+
{
14+
int size = q.size();
15+
vector<int> level ;
16+
for(int i = 0 ; i < size ; i++)
17+
{
18+
TreeNode* temp = q.front();
19+
q.pop();
20+
if(temp->left != NULL) q.push(temp->left);
21+
if(temp->right!= NULL) q.push(temp->right);
22+
level.push_back(temp->val);
23+
}
24+
res.push_back(level);
25+
}
26+
return res ;
27+
}
28+
};

0 commit comments

Comments
 (0)