This problem is from the book Cracking the Coding Interview, and I have trouble understanding the space complexity specified for their solution.
Problem: You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum to a given value. Note that a path can start 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's 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();
}
My Question:
According to the solution, the space complexity of this solution is O(n*log(n))
. However, I feel like the space complexity should be O(log(n))
which represents the depth of recursion stack for the findSum()
function. Why is my analysis wrong? Why is the space complexity O(n*log(n))
?
The tree is not necessarily full - so it could have O(n) depth. As far as I can tell, the space complexity is O(n).