自练代码:
#include <bits/stdc++.h>
using namespace std;
const int maxn= 100 ;
struct node{
int data;
node * rchild, * lchild;
} ;
int k, pre[ maxn] = { 0 } , in[ maxn] = { 0 } , num= 0 ;
node* create ( int prel, int prer, int inl, int inr) {
if ( prel> prer) return NULL ;
node* root= new node;
root- > data= pre[ prel] ;
root- > lchild= root- > rchild= NULL ;
int k;
for ( k= inl; k<= inr; k++ ) {
if ( in[ k] == abs ( pre[ prel] ) ) break ;
}
int numleft= k- inl;
root- > lchild= create ( prel+ 1 , prel+ numleft, inl, k- 1 ) ;
root- > rchild= create ( prel+ numleft+ 1 , prer, k+ 1 , inr) ;
return root;
}
int getNum ( node * root) {
if ( root == NULL ) return 0 ;
int l = getNum ( root- > lchild) ;
int r = getNum ( root- > rchild) ;
return root- > data > 0 ? max ( l, r) + 1 : max ( l, r) ;
}
bool judge5 ( node * root) {
if ( root == NULL ) return true ;
int l = getNum ( root- > lchild) ;
int r = getNum ( root- > rchild) ;
if ( l != r) { return false ; }
return judge5 ( root- > lchild) && judge5 ( root- > rchild) ;
}
bool judge4 ( node * root) {
if ( root== NULL ) return true ;
if ( root- > data< 0 ) {
if ( root- > lchild!= NULL && root- > lchild- > data< 0 ) return false ;
if ( root- > rchild!= NULL && root- > rchild- > data< 0 ) return false ;
}
return judge4 ( root- > lchild) && judge4 ( root- > rchild) ;
}
int main ( ) {
scanf ( "%d" , & k) ;
for ( int i= 0 ; i< k; i++ ) {
int n;
scanf ( "%d" , & n) ;
for ( int j= 0 ; j< n; j++ ) {
scanf ( "%d" , & pre[ j] ) ;
in[ j] = abs ( pre[ j] ) ;
if ( j== 0 && pre[ j] < 0 || ( pre[ j] == 0 ) ) {
printf ( "No\n" ) ;
break ;
}
}
sort ( in, in+ n) ;
node* root= NULL ;
root= create ( 0 , n- 1 , 0 , n- 1 ) ;
if ( judge4 ( root) == true && judge5 ( root) == true ) printf ( "Yes\n" ) ;
else printf ( "No\n" ) ;
fill ( pre, pre+ maxn, 0 ) ;
fill ( in, in+ maxn, 0 ) ;
}
return 0 ;
}
模块自练:
#include <bits/stdc++.h>
using namespace std;
const int maxn= 200 ;
struct node{
int data;
node * rchild, * lchild;
} ;
bool judge4 ( node* root) {
if ( root== NULL ) return true ;
if ( root- > data< 0 ) {
if ( root- > lchild!= NULL && root- > lchild- > data< 0 ) return false ;
if ( root- > rchild!= NULL && root- > rchild- > data< 0 ) return false ;
}
return judge4 ( root- > lchild) && judge4 ( root- > rchild) ;
}
int getnum ( node* root) {
if ( root== NULL ) return 0 ;
int l= getnum ( root- > lchild) ;
int r= getnum ( root- > rchild) ;
return root- > data> 0 ? max ( l, r) + 1 : max ( l, r) ;
}
bool judge5 ( node* root) {
if ( root== NULL ) return true ;
int l= getnum ( root- > lchild) ;
int r= getnum ( root- > rchild) ;
if ( l!= r) return false ;
return judge5 ( root- > lchild) && judge5 ( root- > rchild) ;
}
int main ( ) {
return 0 ;
}
附上某位大神(薛玉洁)代码:
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector< int > pre, post, arr;
struct node {
int val;
struct node * left, * right;
} ;
node* build ( node * root, int v) {
if ( root == NULL ) {
root = new node ( ) ;
root- > val = v;
root- > left = root- > right = NULL ;
} else if ( abs ( v) <= abs ( root- > val) )
root- > left = build ( root- > left, v) ;
else
root- > right = build ( root- > right, v) ;
return root;
}
void getPost ( int root, int end) {
if ( root > end) return ;
int i = root + 1 , j = end;
while ( i <= end && pre[ root] > pre[ i] ) i++ ;
while ( j >= root + 1 && pre[ root] <= pre[ j] ) j-- ;
if ( i != j + 1 ) return ;
getPost ( root + 1 , j) ;
getPost ( i, end) ;
post. push_back ( pre[ root] ) ;
}
bool judge1 ( node * root) {
if ( root == NULL ) return true ;
if ( root- > val < 0 ) {
if ( root- > left != NULL && root- > left- > val < 0 ) return false ;
if ( root- > right != NULL && root- > right- > val < 0 ) return false ;
}
return judge1 ( root- > left) && judge1 ( root- > right) ;
}
int getNum ( node * root) {
if ( root == NULL ) return 0 ;
int l = getNum ( root- > left) ;
int r = getNum ( root- > right) ;
return root- > val > 0 ? max ( l, r) + 1 : max ( l, r) ;
}
bool judge2 ( node * root) {
if ( root == NULL ) return true ;
int l = getNum ( root- > left) ;
int r = getNum ( root- > right) ;
if ( l != r) return false ;
return judge2 ( root- > left) && judge2 ( root- > right) ;
}
int main ( ) {
int k, n;
scanf ( "%d" , & k) ;
for ( int i = 0 ; i < k; i++ ) {
scanf ( "%d" , & n) ;
arr. resize ( n) ;
pre. resize ( n) ;
node * root = NULL ;
for ( int j = 0 ; j < n; j++ ) {
scanf ( "%d" , & arr[ j] ) ;
root = build ( root, arr[ j] ) ;
pre[ j] = abs ( arr[ j] ) ;
}
post. clear ( ) ;
getPost ( 0 , n- 1 ) ;
if ( post. size ( ) != n || arr[ 0 ] < 0 || judge1 ( root) == false || judge2 ( root) == false )
printf ( "No\n" ) ;
else
printf ( "Yes\n" ) ;
}
return 0 ;
}