This commit is contained in:
chefyuan
2021-05-13 14:29:00 +08:00
2 changed files with 55 additions and 0 deletions

View File

@@ -350,6 +350,8 @@ class Solution {
**测试题目: leetcode 102 二叉树的层序遍历**
Java Code:
```java
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
@@ -378,6 +380,33 @@ class Solution {
}
```
C++ Code:
```cpp
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if (!root) return res;
queue<TreeNode *> q;
q.push(root);
while (!q.empty()) {
vector <int> t;
int size = q.size();
for (int i = 0; i < size; ++i) {
TreeNode * temp = q.front();
q.pop();
if (temp->left != nullptr) q.push(temp->left);
if (temp->right != nullptr) q.push(temp->right);
t.emplace_back(temp->val);
}
res.push_back(t);
}
return res;
}
};
```
时间复杂度On 空间复杂度On
大家如果吃透了二叉树的层序遍历的话可以顺手把下面几道题目解决掉思路一致甚至都不用拐弯