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