6-6 CheckBST[1] (30 分)
Sample Input 1: (for the following tree)
4
Sample Output 1:
Yes. Key = 5
Sample Input 2: (for the following tree)
3
Sample Output 2:
No. Height = 3
误区:判断BST要用中序遍历,看是否递增。
而不能遍历每个节点,判断其左右节点是否满足。
1、二叉排序树的定义
二叉排序树(Binary Sort Tree)又称二叉查找(搜索)树(Binary Search Tree)。
其定义为:
二叉排序树或者是空树,或者是满足如下性质的二叉树:
①若它的左子树非空,则左子树上所有结点的值均小于根结点的值;
②若它的右子树非空,则右子树上所有结点的值均大于根结点的值;
③左、右子树本身又各是一棵二叉排序树。
之前写的
//wrong,judge bst fail
//24分,一个点过不去(判断bst)
//模拟数组bfs,用struct存所有节点,判断最大depth,排序节点,获得K-th max;
struct q {
BinTree Te;
int dep;
}q[1000];
int cmp(const void* a,const void* b) {
return (*(struct q*)a).Te->Key < (*(struct q*)b).Te->Key;
}
int CheckBST(BinTree T, int K) {
int sor[1000];