Skip to content

Commit 44249b2

Browse files
authored
Create tree.cpp
1 parent c807e5b commit 44249b2

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

0000.jian_zhi_offer/32_2/tree.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
};

0 commit comments

Comments
 (0)