257. Binary Tree Paths

2016-12-16  本文已影响0人  夜皇雪
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res=new ArrayList<>();
        if(root==null) return res;
        helper(res,root,"");
        return res;
    }
    public void helper(List<String> res,TreeNode root,String s){
        if(root.left==null&&root.right==null) res.add(s+root.val);
        if(root.left!=null) helper(res,root.left,s+root.val+"->");
        if(root.right!=null) helper(res,root.right,s+root.val+"->");
    }
}
上一篇 下一篇

猜你喜欢

热点阅读