简单的建树,然后遍历
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef struct BTREENODE_ {
int lw, ld, rw, rd;
struct BTREENODE_ *left_child;
struct BTREENODE_ *right_child;
}BTREENODE;
int flag;
void build_binary_tree(BTREENODE **node)
{
(*node) = new BTREENODE; (*node)->left_child = (*node)->right_child = NULL;
scanf("%d %d %d %d", &(*node)->lw, &(*node)->ld, &(*node)->rw, &(*node)->rd);
if( !(*node)->lw ) {
build_binary_tree(&(*node)->left_child);
}
if( !(*node)->rw ) {
build_binary_tree(&(*node)->right_child);
}
}
int is_equilibrium(BTREENODE *node)
{
if( !flag ) {
return -1;
}
node->lw = (node->lw)? node->lw : is_equilibrium(node->left_child);
node->rw = (node->rw)? node->rw : is_equilibrium(node->right_child);
if( node->lw*node->ld != node->rw*node->rd ) {
flag = 0;
}
return node->lw+node->rw;
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
freopen("test.in", "r", stdin);
#endif
int cas;
BTREENODE *root;
scanf("%d", &cas);
for(int i = 0; i < cas; i ++) {
build_binary_tree(&root);
flag = 1; is_equilibrium(root);
if( i ) {
printf("\n");
}
if( flag ) {
printf("YES\n"); continue;
}
printf("NO\n");
}
return 0;
}
uva_839_Not so Mobile
最新推荐文章于 2021-09-06 10:14:39 发布