添加 (0102.二叉树的层序遍历.md):102.二叉树的层序遍历 增加 c++ 递归版本

This commit is contained in:
yangtzech
2022-05-11 21:30:43 +08:00
committed by GitHub
parent cf4c25b891
commit 923ce563f1

View File

@ -82,6 +82,26 @@ public:
} }
}; };
``` ```
```CPP
# 递归法
class Solution {
public:
void order(TreeNode* cur, vector<vector<int>>& result, int depth)
{
if (cur == nullptr) return;
if (result.size() == depth) result.push_back(vector<int>());
result[depth].push_back(cur->val);
order(cur->left, result, depth + 1);
order(cur->right, result, depth + 1);
}
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;
int depth = 0;
order(root, result, depth);
return result;
}
};
```
python3代码 python3代码