首页 > 其他 > 详细

[LeetCode]Binary Tree Paths

时间:2015-11-29 10:40:57      阅读:320      评论:0      收藏:0      [点我收藏+]
public class Solution {
    List<String> result = new ArrayList<String>();
    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) {
            return result;
        }
        helper(root, "");
        return result;
    }
    public void helper(TreeNode root, String str) {
       str = str + String.valueOf(root.val);
       if (root.left == null && root.right == null) {
           result.add(str);
           return;
       }
       str = str + "->";
       if (root.left != null) {
           helper(root.left, str);
       } 
       if (root.right != null) {
           helper(root.right, str);
       }
    }
}

 

[LeetCode]Binary Tree Paths

原文:http://www.cnblogs.com/vision-love-programming/p/5004381.html

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