fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. struct Node{
  6. int val;
  7. Node *left,*right;
  8. Node(int x){
  9. val=x;
  10. left=right=NULL;
  11. }
  12. };
  13. void makeNode(Node *root, int u,int v, char c){
  14. if(c=='L') root->left= new Node(v);
  15. else root->right= new Node(v);
  16.  
  17. }
  18.  
  19. void insert(Node *root, int u,int v,char c){
  20. if(root==NULL) return;
  21. if(root->val==u){
  22. makeNode(root,u,v,c);
  23. }
  24. insert(root->left,u,v,c);
  25. insert(root->right,u,v,c);
  26. }
  27.  
  28. int height(Node *root){
  29. if(root==NULL) return 0;
  30. return 1+max(height(root->left),height(root->right));
  31. }
  32.  
  33. bool check(Node *root, int level,int h){
  34. if(root==NULL) return true;
  35. if(root->left==NULL && root->right==NULL && level<h) return false;
  36. return check(root->left,level+1,h) && check(root->right,level+1,h);
  37.  
  38. }
  39.  
  40. int main(){
  41. int t;cin>>t;
  42. while(t--){
  43. int n; cin>>n;
  44. Node* root=NULL;
  45. while(n--){
  46. int u,v; char c;
  47. cin>>u>>v>>c;
  48. if(root==NULL){
  49. root = new Node(u);
  50. makeNode(root,u,v,c);
  51. }
  52. else insert(root, u,v,c);
  53. }
  54. if(check(root,1,height(root))) cout<<1<<endl;
  55. else cout<<0<<endl;
  56. }
  57. }
Success #stdin #stdout 0.01s 5316KB
stdin
2

2
1 2 R 1 3 L
4
10 20 L 10 30 R 20 40 L 20 60 R
stdout
1
0