mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 (0102.二叉树的层序遍历.md):102.二叉树的层序遍历 增加 c++ 递归版本
This commit is contained in:
@ -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代码:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user