Simple java solution


  • 1
    H
    List<String> list = new ArrayList<>();
    public int sumNumbers(TreeNode root) {
        if (root == null) return 0;
        int res = 0;
        helper(root, "");
        for (String item: list) {
            res += Integer.parseInt(item);
        }
        return res;
    }
    private void helper(TreeNode root, String sum) {
        if (root.left == null && root.right == null) list.add(sum + root.val);
        else {
            if (root.left != null) helper(root.left, sum + root.val);
            if (root.right != null) helper(root.right, sum + root.val);
        }
    }

Log in to reply
 

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