首页 > 其他 > 详细

30 Day Challenge Day 15 | Leetcode 257. Binary Tree Paths

时间:2020-09-30 17:02:37      阅读:38      评论:0      收藏:0      [点我收藏+]

题解

Easy | DFS

class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> paths;
        dfs(root, "", paths);
        return paths;
    }
    
    void dfs(TreeNode* node, string path, vector<string>& paths) {
        if(!node) return;
        
        if(!node->left && !node->right) {
            path += to_string(node->val);
            paths.push_back(path);
            return;
        }
        
        path += to_string(node->val) + "->";

        dfs(node->left, path, paths);
        dfs(node->right, path, paths);
    }
};

30 Day Challenge Day 15 | Leetcode 257. Binary Tree Paths

原文:https://www.cnblogs.com/casperwin/p/13754540.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!