-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevelOrder.cpp
More file actions
executable file
·35 lines (34 loc) · 1022 Bytes
/
levelOrder.cpp
File metadata and controls
executable file
·35 lines (34 loc) · 1022 Bytes
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
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
vector<vector<int> > levelOrder(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int> > out;
if(root==NULL) return out;
vector<TreeNode*> nodelist;
nodelist.push_back(root);
while(!nodelist.empty()){
vector<int> temp;
int len = nodelist.size();
for(int i=0;i<len;i++)
{
temp.push_back(nodelist[i]->val);
if(nodelist[i]->left)
nodelist.push_back(nodelist[i]->left);
if(nodelist[i]->right)
nodelist.push_back(nodelist[i]->right);
}
out.push_back(temp);
nodelist.erase(nodelist.begin(),nodelist.begin()+len);
cout<< nodelist.size()<<" ";
//break;
}
return out;
}