【发布时间】:2017-02-05 21:19:35
【问题描述】:
我想不出一个递归算法来做到这一点。我的尝试是:
void capValue(Node node) {
if (node == null)
return
if (node.element > cap)
capValue(node.left)
node = null;
else // node.element < cap
capValue(node.right)
}
但是,您不能只清空节点(至少在 java 中,我想在其中编写代码),因为这只会将当前指针移动到地址 0,而我们想要摆脱的对象仍然有一个通过树根指向它的“指针路径”,因此不会被垃圾收集。
【问题讨论】:
标签: java algorithm binary-tree binary-search-tree graph-theory