Tuesday, November 3, 2015

[leetcode]Binary Tree Paths

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
public class Solution {
    List<String> result = new ArrayList<String>();
    public List<String> binaryTreePaths(TreeNode root) {
        rec(root, "");
        return result;
    }
    
    private void rec(TreeNode root, String str){
        if (root == null) return;
        if (root.left == null && root.right == null){
            result.add(str+root.val);
        }
        rec(root.left, str+root.val+"->");
        rec(root.right, str+root.val+"->");
    }
}

No comments:

Post a Comment