40WA除了满二叉树的各种点求解

P5018 [NOIP2018 普及组] 对称二叉树

@[ChengZe](/space/show?uid=110694) dfs和判对称分开来 ```cpp #include <bits/stdc++.h> #define rep(i,a,b) for(i=a;i<b;i++) #define per(i,b,a) for(i=b;i>=a;i--) #define For(i,a,b) for(i=a;i<=b;i++) #define Forenska(it,c) for(register __typeof(c.begin()) it=c.begin();it!=c.end();it++) #define pb push_back #define mp make_pair #define mt make_tuple #define fi first #define se second #define all(x) x.begin(),x.end() #define sqr(x) ((x)*(x)) #define lowbit(x) ((x)&(-x)) #define GREATER(x) x,vector<x>,greater<x> using namespace std; int i,j; typedef long long LL; typedef pair<int,int> pii; typedef pair<LL,LL> pLL; typedef vector<int> vec; typedef vector<LL> vecL; typedef vector<pii> vecP; typedef vector<pLL> vecPL; typedef vector<string> vecS; typedef vector<vec> mat; typedef complex<double> point; const long double PI=3.14159265358979323846264338327950288; const LL INFLL=0x3f3f3f3f3f3f3f3f; const int INF=0x3f3f3f3f; const long double EPS=1e-10; int read() { int x=0; char ch=' '; bool flag=false; while(!isdigit(ch)) { if(ch=='-')flag=true; ch=getchar(); } while(isdigit(ch)) { x=(x<<3)+(x<<1)+ch-'0'; ch=getchar(); } return flag?-x:x; } struct Node { Node *Lson; Node *Rson; int val; int sons; Node() { Lson=Rson=NULL; } }; int ans; int cnt(Node* v) { if(v==NULL)return 0; return v->sons=cnt(v->Lson)+cnt(v->Rson)+1; } bool check(Node* Left,Node* Right) { if(Left==NULL && Right==NULL)return true; else if(Left==NULL || Right==NULL || Left->sons!=Right->sons || Left->val!=Right->val)return false; else return check(Left->Lson,Right->Rson) && check(Left->Rson,Right->Lson); } void dfs(Node *v) { if(v==NULL)return; if(check(v,v) && ans<v->sons)ans=v->sons; dfs(v->Lson); dfs(v->Rson); } int main() { vector <Node> tree; int n=read(); tree.resize(n); rep(i,0,n)tree[i].val=read(); rep(i,0,n) { int Ls=read(),Rs=read(); if(Ls!=-1)tree[i].Lson=&tree[Ls-1]; if(Rs!=-1)tree[i].Rson=&tree[Rs-1]; } cnt(&tree[0]); dfs(&tree[0]); cout<<ans<<endl; return 0; } ```
by Smile_Cindy @ 2019-02-01 10:44:40


|