【发布时间】:2012-12-11 04:08:15
【问题描述】:
删除binary tree 中所有节点的标准算法在这些节点上使用postorder traversal:
if (root is not null) {
recursively delete left subtree
recursively delete right subtree
delete root
}
该算法使用 O(h) 辅助存储空间,其中 h 是树的高度,因为在递归调用期间需要存储堆栈帧的空间。但是,它运行时间为 O(n),因为每个节点都只被访问过一次。
是否有一种算法可以在不牺牲运行时间的情况下仅使用 O(1) 辅助存储空间删除二叉树中的所有节点?
【问题讨论】:
标签: algorithm data-structures binary-tree big-o space-complexity