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);
}
}
Leetcode 257. Binary Tree Paths
最新推荐文章于 2023-11-12 18:07:09 发布