#include

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) && (cur < upper_bound) && (is_bst( a[root].left_child, lower_bound, cur ) == 1) && (is_bst( a[root].right_child, cur, upper_bound ) == 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( 1 , -INFINITE, INFINITE ) << endl; return(0); }

二叉查找树判断算法 - C++ 实现

原文地址: https://www.cveoy.top/t/topic/oYNU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录