【发布时间】:2018-02-22 22:46:21
【问题描述】:
这是我的程序,用于在预购中将 BST 的叶节点作为数组给出。 该程序以某种方式只打印第一页而不是全部。我已经尝试找到错误但无法找到。这是程序:
//method
public static boolean isLeaf(int pre[], int i, int n,
int min, int max)
{
if (i >= n)
return false;
if(pre[i] > min && pre[i] < max) {
i++;
boolean left = isLeaf(pre, i, n, min, pre[i-1]);
boolean right = isLeaf(pre, i, n, pre[i-1], max);
if (!right && !left)
System.out.println(pre[i-1] );
return true;
}
return false;
}
//Test case:
public static void main(String[] args){
int preorder[] = { 890, 325, 290, 530, 965 };
int n = preorder.length;
int i=0;
isLeaf( preorder, i, n,
Integer.MIN_VALUE, Integer.MAX_VALUE);
}
//it just prints 290, it should print 290, 530, 965 instead
【问题讨论】:
-
这里是树的前/后/顺序遍历的链接,您可能会发现它很有用geeksforgeeks.org/…
-
你是如何从你的数组中定义你的 BST 的?
-
数组作为 BST 的预排序遍历给出
标签: java recursion binary-search-tree preorder