LeetCode257. Binary Tree Paths(dfs)

来源:互联网 发布:类似openvpn的软件 编辑:程序博客网 时间:2024/06/05 03:15

题目链接

输出dfs二叉树路径。

刚刷leetcode,跟ACM提交方式不一样,适应中。。。

class Solution {public:    vector<string> binaryTreePaths(TreeNode* root) {        vector<string> ans;        if(root == NULL) return ans;        dfs(ans, root, to_string(root->val));        return ans;    }    void dfs(vector<string> &ans, TreeNode* root, string s) {        if(root->left==NULL && root->right==NULL){            ans.push_back(s);            return;        }        if(root->left) dfs(ans, root->left, s+"->"+to_string(root->left->val));        if(root->right) dfs(ans, root->right, s+"->"+to_string(root->right->val));    }};
0 0
原创粉丝点击