Skip to content

Commit eac1612

Browse files
committed
Binary Tree Level Oder Traversal solution
1 parent bbc9da3 commit eac1612

File tree

1 file changed

+35
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)