不是VIP会员,不能显示答案

题目解答

题目:
(二叉查找树) 二叉查找树具有如下性质:每个节点的值都大于其左子树上所有节点的值、小于其右 子树上所有节点的值。试判断一棵树是否为二叉查找树。
输入的第一行包含一个整数 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 ( cur<upper_bound ) and //(3 分) 
   (is_bst(a[root].left_child, lower_bound, cur) = 1) and 
   (is_bst( a[root].right_child , cur , upper_bound ) = 1) then is_bst := 1//(3 分,3 分,3 分) 
     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( 1 , -INFINITE, INFINITE));//(2 分) 
end. 
考点:
分析:
解答:
评论:
老师: