【发布时间】:2013-01-04 02:15:21
【问题描述】:
我目前正在使用以下代码按顺序遍历 BST。我的问题是,一旦达到第 k 个最小值,所有计算就会停止。
http://codepad.viper-7.com/XMGcxz
我的问题在于以下功能
public function _kthSmallest($node, $k){
if($node->left != NULL){
$this->_kthSmallest($node->left, $k);
}
echo $node->data . ' ';
self::$counter++;
echo self::$counter . "<br/>";
/*
if(self::$counter >= $k){
return $node->data;
}
*/
if($node->right != NULL){
$this->_kthSmallest($node->right, $k);
}
}
如果我取消注释这段代码,我会遇到问题,因为根节点总是被打印出来。
/*
if(self::$counter >= $k){
return $node->data;
}
*/
关于在我达到第 k 个最小后如何停止的任何想法?目前该功能在整个 BST 中持续运行。
【问题讨论】:
-
这是程序代码。为什么它被标记为 OOP?
标签: php data-structures