839 - Not so Mobile

来源:互联网 发布:电子竞技 奥运会 知乎 编辑:程序博客网 时间:2024/05/24 04:24
/*解题思路:创建二叉树遍历检查这道题其实不需要建立树的结构,因为遍历检查可以在创建时就能完成需要用到数组做存储结构*/#include <iostream>#include <string>#include <cstdio>#include <cstdlib>using namespace std;struct Node{int w1,d1,w2,d2;Node *lchild,*rchild;};bool isEqual;Node *createTree(){int w1,d1,w2,d2;cin>>w1>>d1>>w2>>d2;Node *root=(Node *)malloc(sizeof(Node));root->w1=w1;root->d1=d1;root->w2=w2;root->d2=d2;if(w1==0)root->lchild=createTree();elseroot->lchild=NULL;if(w2==0)root->rchild=createTree();elseroot->rchild=NULL;return root;}int check(Node *root){if(root->lchild!=NULL)root->w1=check(root->lchild);if(root->rchild!=NULL)root->w2=check(root->rchild);if(isEqual==true)//这里有个技巧******isEqual=(root->w1*root->d1==root->w2*root->d2);/*另一种表示形式if(root->w1*root->d1!=root->w2*root->d2)isEqual=false;*/return root->w1+root->w2;}int main(){//freopen("data.in","r",stdin);int T;cin>>T;while(T--){Node *root=createTree();isEqual=true;check(root);cout<<(isEqual?"YES":"NO")<<endl;//The outputs of two consecutive cases will be separated by a blank line.if(T!=0)//仔细看题,WA原因cout<<endl;}return 0;}


原创粉丝点击