Find all root to leaf paths in binary tree Description:

Given a binary tree, return all root-to-leaf paths.

Leetcode


/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */



Code:

class Solution {

    public List&lt;String&gt; binaryTreePaths(TreeNode root) {
        List&lt;String&gt; paths = new ArrayList&lt;&gt;();
        traverse(root, new ArrayList&lt;&gt;(), paths);
        return paths;
    }

    private void traverse(TreeNode root, List&lt;String&gt; path, List&lt;String&gt; paths) {
        if (root == null) return;

        path.add(""+root.val);
        if (root.left == null &amp;&amp; root.right == null) {
            paths.add(String.join("-&gt;", path));
        }

        traverse(root.left,  path, paths);
        traverse(root.right, path, paths);

        path.remove(path.size() - 1);
    }
}
regarding the translation from int to String

path.add(""+root.val);


This is both unclear and also involves unnecessary String creation.  Why not use the "official" conversion method? it clearly states the intention and is more efficient

path.add(String.valueOf(root.val));