Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by fvdfbg ( 5 years ago )
#include<bits/stdc++.h>
using namespace std;

int u,v,n;
char x;
struct node
{
    int data;
    node *left;
    node *right;
    node(int x)
    {
        data = x;
        left = right = NULL;
    }
};

bool Check(node *rt)
{
    queue<node *> qu;
    //stack<int> s;
    qu.push(rt);
    int l=0;
    while (!qu.empty())
    {
        int cnt = qu.size();
        if (cnt != pow(2,l))
            return false;
        for (int i=0;i<cnt;i++)
        {
            node *temp = qu.front();
            //s.push(tmp->data);
            qu.pop();
            if (temp->left != NULL) qu.push(temp->left);
            if (temp->right != NULL) qu.push(temp->right);     
        }
        l++;
    }
    return true;
}

void solve()
{
    cin >> n;
    node *rt = NULL;
    node *con;
    map<int , node*> a;
    for (int i=0;i<n;i++)
    {
        node *bo;
        cin >> u >> v >> x;
        if (a.find(u) == a.end())
        {
            bo = new node(u);
            a[u] = bo;
            if (rt == NULL)
                rt = bo;
        }
        else 
            bo = a[u];
       con = new node(v);
        if (x == 'L')
            bo->left = con;
        else 
            bo->right = con;
        a[v] = con;            
    }
    if (Check(rt))
        cout << "1" << endl;
    else 
        cout << "0" << endl;    
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

 

Revise this Paste

Your Name: Code Language: