美文网首页
257. Binary Tree Paths

257. Binary Tree Paths

作者: 夜皇雪 | 来源:发表于2016-12-16 03:42 被阅读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+"->");
    }
}

相关文章

网友评论

      本文标题:257. Binary Tree Paths

      本文链接:https://www.haomeiwen.com/subject/ujfnmttx.html