Leetcode 257. Binary Tree Paths

来源:互联网 发布:java常用api详解 编辑:程序博客网 时间:2024/06/03 23:56
public class Solution {    public List<String> binaryTreePaths(TreeNode root) {        List<String> ret = new ArrayList<>();        helper(root, ret, "");        return ret;    }        public static void helper(TreeNode t, List<String> ret, String tmp) {        if (t == null) return;        // found a leaf, end of a path        if (t.left == null && t.right == null) {            tmp += t.val;            ret.add(new String(tmp));            return;        }        tmp += t.val;        tmp += "->";        helper(t.left, ret, tmp);        helper(t.right, ret, tmp);    }}

0 0
原创粉丝点击