/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string>ans;//存放路径;
void dfs(string path,TreeNode*root)
{
if(!root)return;
if(path.size()) path+="->";
path+=to_string(root->val);
if(!root->left&&!root->right) ans.push_back(path);
dfs(path,root->left);
dfs(path,root->right);
}
vector<string> binaryTreePaths(TreeNode* root) {
string path;//建立路径;
dfs(path,root);
return ans;
}
};
leetcode 257. 二叉树的所有路径
最新推荐文章于 2024-05-02 11:51:02 发布