【发布时间】:2015-02-03 16:36:46
【问题描述】:
存储路径的空间复杂度是多少,即。从根到数组中特定叶子的二叉树的节点?
基本上,我正在寻找以下算法的空间复杂度:
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);
}
【问题讨论】:
标签: java algorithm binary-tree complexity-theory space-complexity