值、小于其右子树上所有节点的值。试判断一棵树是否为二叉查找树。
输入的第一行包含一个整数 n,表示这棵树有 n 个顶点,编号分别为 1, 2, …, n,其
中编号为 1 的为根结点。之后的第 i 行有三个数 value, left_child, right_child,分别表示
该节点关键字的值、左子节点的编号、右子节点的编号;如果不存在左子节点或右子节
点,则用 0 代替。输出 1 表示这棵树是二叉查找树,输出 0 则表示不是。
program Bst; const SIZE = 100; const INFINITE = 1000000; type node = record left_child, right_child, value : longint; end; var a : array[1..SIZE] of node; i, n : longint; function is_bst(root, lower_bound, upper_bound : longint) : longint; var cur : longint; begin if root = 0 then begin is_bst := 1; exit; end; cur := a[root].value; if (cur > lower_bound) and (1) and //(3 分) (is_bst(a[root].left_child, lower_bound, cur) = 1) and (is_bst(2, 3, 4) = 1) then //(3 分,3 分,3 分) is_bst := 1 else is_bst := 0; end; begin readln(n); for i := 1 to n do read(a[i].value, a[i].left_child, a[i].right_child); writeln(is_bst(5, -INFINITE, INFINITE)); //(2 分) end.