码迷,mamicode.com
首页 > 其他好文 > 详细

257.Binary Tree Paths

时间:2016-06-22 10:46:17      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

    /*
     * 257.Binary Tree Paths 
     * 2016-6-21 By mingyang 
     * 这个题目吧,刚开始想用backtracking来做,后面看网上都不用回溯,因为可以直接传string这个参数
     * 原理都一样的,而且更简单了。
     */
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<String>();
        if (root != null) 
        dfs(root, "", res);
        return res;
    }
    private void dfs(TreeNode root, String path, List<String> res) {
        if (root.left == null && root.right == null) res.add(path + root.val);
        if (root.left != null) dfs(root.left, path + root.val + "->", res);
        if (root.right != null) dfs(root.right, path + root.val + "->", res);
    }

 

257.Binary Tree Paths

标签:

原文地址:http://www.cnblogs.com/zmyvszk/p/5606017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!