0
我正在学习二叉搜索树。我想返回二叉查找树的有序遍历的第k个元素。我怎样才能保持变量'计数'更新或有一些方法可以打破循环,一旦我找到第k个元素并将其打印出来?在二叉搜索树中找到第K个元素
public void kthElement(int n, int count, BinaryNode<AnyType> root){
if(root.left !=null)
this.kthElement(n, count, root.left);
count++;
if(count==n){
System.out.println(root.element);
}
else if(count!=n){
return;}
if(root.right != null)
this.kthElement(n, count, root.right);
}
好吧,我想出了使用第一个建议......采取了一点思考(和朋友的帮助) – user2130688 2013-03-27 23:29:15