File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution
2
+ {
3
+ public:
4
+ vector<vector<int >> levelOrder (TreeNode* root)
5
+ {
6
+ std::queue<TreeNode*> tree;
7
+ if (NULL != root)
8
+ tree.push (root);
9
+ std::vector<vector<int >> rst;
10
+ std::vector<int > temp;
11
+ temp.resize (1000 );
12
+ temp.clear ();
13
+ auto last = root;
14
+ auto newLast = last;
15
+ while (!tree.empty ())
16
+ {
17
+ auto current = tree.front ();
18
+ temp.push_back (current->val );
19
+ tree.pop ();
20
+ if (current->left != NULL )
21
+ {
22
+ tree.push (current->left );
23
+ newLast = current->left ;
24
+ }
25
+ if (current->right != NULL )
26
+ {
27
+ tree.push (current->right );
28
+ newLast = current->right ;
29
+ }
30
+ if (current == last)
31
+ {
32
+ last = newLast;
33
+ rst.push_back (temp);
34
+ temp.clear ();
35
+ }
36
+ }
37
+ return rst;
38
+ }
39
+ };
You can’t perform that action at this time.
0 commit comments