0 of 2 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 2 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
1、二叉查找树具有如下性质:每个节点的值都大于其左子树上所有节点的值、小于其右子 树上所有节点的值。那么,二叉查找树的( )是一个有序序列。 (2013年提高组)
2、完善程序:(2013年普及组)
(二叉查找树) 二叉查找树具有如下性质: 每个节点的值都大于其左子树上所有节点的值、小于其右子树上所有节点的值。试判断一棵树是否为二叉查找树。
输入的第一行包含一个整数 n,表示这棵树有 n 个顶点, 编号分别为 1,2,…,n,其中编号为 1 的为根结点。之后的第 i 行有三个数value, left_child, right_child ,分别表示该节点关键字的值、左子节点的编号、右子节点的编号;如果不存在左子节点或右子节点,则用0代替。输出1 表示这棵树是二叉查找树,输出0 则表示不是。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <iostream> using namespace std; const int SIZE = 100; const int INFINITE = 1000000; struct node { int left_child, right_child, value; }; node a[SIZE]; int is_bst( int root, int lower_bound, int upper_bound ) { int cur; if ( root == 0 ) return(1); cur = a[root].value; if ( (cur > lower_bound) && ( ① ) && (is_bst( a[root].left_child, lower_bound, cur ) == 1) && (is_bst( ②, ③, ④ ) == 1) ) return(1); return(0); } int main() { int i, n; cin >> n; for ( i = 1; i <= n; i++ ) cin >> a[i].value >> a[i].left_child >> a[i].right_child; cout << is_bst( ⑤, -INFINITE, INFINITE ) << endl; return(0); } |
填空一: 填空二: 填空三: 填空四: 填空五: