My java solution very easy to understand


  • 0
    F
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> result = new LinkedList<>();
        if (root == null) {
            return result;
        } else {
            if (root.left == null && root.right == null) {
                result.add(Integer.toString(root.val));
            } else {
                List<String> left = binaryTreePaths(root.left);
                List<String> right = binaryTreePaths(root.right);
                result.addAll(left.stream().map(tmp -> root.val + "->" + tmp).collect(Collectors.toList()));
                result.addAll(right.stream().map(tmp -> root.val + "->" + tmp).collect(Collectors.toList()));
            }
            return result;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.