What is the space complexity of storing a path ie. nodes of a binary tree from root to a particular leaf in an array?
Basically, I am looking for space complexity of the following algorithm:
public void printPath () {
doPrint(root, new ArrayList<TreeNode>());
}
private void doPrint(TreeNode node, List<TreeNode> path) {
if (node == null) return;
path.add(node);
if (node.left == null && node.right == null) {
System.out.println("Path from root: " + root.item + " to leaf: " + node.item + " - ");
for (TreeNode treeNode : path) {
System.out.print(treeNode.item + " ");
}
System.out.println();
}
doPrint(node.left , path);
doPrint(node.right, path);
path.remove(path.size() - 1);
}