[LeetCode]Binary Tree Paths

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);
       }
    }
}

 

posted @ 2015-11-29 10:05  Weizheng_Love_Coding  阅读(143)  评论(0编辑  收藏  举报