Skip to content

Commit 2f93235

Browse files
authored
102. Binary Tree level Order Traversal
1 parent 24e34ef commit 2f93235

File tree

1 file changed

+31
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)