How to calculate the complexity of the space for printing all the paths that are summed with a given value in a binary tree

This problem is from Cracking the Coding Interview, and it’s hard for me to understand the complexity of the space indicated for solving them.

Problem: You are given a binary tree in which each node contains a value. Create an algorithm to print all paths that add up to the given value. Note that the path can begin or end anywhere in the tree.

Solution (in Java):

public static void findSum(TreeNode node, int sum, int[] path, int level) {
    if (node == null) {
        return;
    }

    /* Insert current node into path */
    path[level] = node.data; 

    int t = 0;
    for (int i = level; i >= 0; i--){
        t += path[i];
        if (t == sum) {
            print(path, i, level);
        }
    }

    findSum(node.left, sum, path, level + 1);
    findSum(node.right, sum, path, level + 1);

    /* Remove current node from path. Not strictly necessary, since we would
     * ignore this value, but it good practice.
     */
    path[level] = Integer.MIN_VALUE; 
}

public static int depth(TreeNode node) {
    if (node == null) {
        return 0;
    } else {
        return 1 + Math.max(depth(node.left), depth(node.right));
    }
}

public static void findSum(TreeNode node, int sum) {
    int depth = depth(node);
    int[] path = new int[depth];
    findSum(node, sum, path, 0);
}

private static void print(int[] path, int start, int end) {
    for (int i = start; i <= end; i++) {
        System.out.print(path[i] + " ");
    }
    System.out.println();
}

: , O(n*log(n)). , , O(log(n)), findSum(). ? O(n*log(n))?

+4
2

, O (n). , O (n).

+1

; , O (log n). ( , , , , , - , .) , . , , , ... , , , .

, , , d, O (log n), , O (n), .

2 , : . int[] depth(node) findSum(TreeNode, int), O (d).

findSum(TreeNode, int) depth(node), :

[findSum(TreeNode, int)] [depth] ... [depth]

depth() , O (d).

, depth() , findSum(TreeNode, int, int[], int).

, , depth(), O (d).

, O (d), O (d), , O (d), O (n ) , O (log n), .

0

All Articles