-
Notifications
You must be signed in to change notification settings - Fork 0
/
107.二叉树的层次遍历-ii.cpp
80 lines (70 loc) · 1.84 KB
/
107.二叉树的层次遍历-ii.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* @lc app=leetcode.cn id=107 lang=cpp
*
* [107] 二叉树的层次遍历 II
*/
// @lc code=start
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#include <vector>
#include <queue>
using namespace std;
// struct TreeNode
// {
// int val;
// TreeNode *left;
// TreeNode *right;
// TreeNode(int x) : val(x), left(NULL), right(NULL) {}
// };
class Solution
{
public:
vector<vector<int>> levelOrderBottom(TreeNode *root)
{
vector<vector<int>> result;
// 层次遍历法 20 ms
// if (!root)
// return result;
// vector<int> item;
// queue<TreeNode *> todo;
// todo.push(root);
// while (!todo.empty())
// {
// int count = todo.size();
// item.clear();
// for (size_t i = 0; i < count; i++)
// {
// TreeNode *node = todo.front();
// todo.pop();
// item.push_back(node->val);
// if (node->left)
// todo.push(node->left);
// if (node->right)
// todo.push(node->right);
// }
// result.insert(result.begin(), item);
// }
// 方法二:递归法 8 ms
levelOrder(result, root, 0);
reverse(result.begin(), result.end());
return result;
}
void levelOrder(vector<vector<int>> &result, TreeNode *node, int level)
{
if (!node)
return;
if (level >= result.size())
result.push_back({});
result[level].push_back(node->val);
levelOrder(result, node->left, level + 1);
levelOrder(result, node->right, level + 1);
}
};
// @lc code=end