LeetCode 257. 二叉树的所有路径
原题链接
简单
作者:
Tracing
,
2020-08-19 23:22:43
,
所有人可见
,
阅读 432
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
function<void(TreeNode*, string)> get_path= [&](TreeNode* root, string path) {
if(root == nullptr) return ;
path += to_string(root->val);
if(root->left == nullptr && root->right == nullptr) {
res.push_back(path);
}
else {
path += "->";
}
get_path(root->left, path);
get_path(root->right, path);
};
get_path(root, "");
return res;
}
private:
vector<string> res;
};