fork download
  1. {
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. struct Node{
  5. int data;
  6. Node *left,*right;
  7. };
  8. Node *newNode(int data)
  9. {
  10. Node* temp = new Node;
  11. temp->data = data;
  12. temp->left = NULL;
  13. temp->right = NULL;
  14.  
  15. return (temp);
  16. }
  17. void insert(Node *root,int a1,int a2,char lr){
  18. if(root==NULL){
  19. return;
  20. }
  21. if(root->data==a1){
  22. switch(lr){
  23. case 'L':root->left=newNode(a2);
  24. break;
  25. case 'R':root->right=newNode(a2);
  26. break;
  27. }
  28. }
  29. else{
  30. insert(root->left,a1,a2,lr);
  31. insert(root->right,a1,a2,lr);
  32. }
  33. }
  34. void inorder(Node *root){
  35. if(root==NULL)
  36. return;
  37. inorder(root->left);
  38. cout<<root->data<<" ";
  39. inorder(root->right);
  40. }
  41. int maxNodeLevel(Node *root1);
  42. int main()
  43. {
  44. //freopen("input.txt","r",stdin);
  45. //freopen("output.txt","w",stdout);
  46. int t;
  47. cin>>t;
  48. while(t-->0)
  49. {
  50. int n;
  51. cin>>n;
  52. int m=n;
  53. Node *root1=NULL;
  54. while(n-->0){
  55. int a1,a2;
  56. cin>>a1>>a2;
  57. char lr;
  58. scanf(" %c",&lr);
  59. if(root1==NULL){
  60. root1=newNode(a1);
  61. switch(lr){
  62. case 'L':root1->left=newNode(a2);
  63. break;
  64. case 'R':root1->right=newNode(a2);
  65. break;
  66. }
  67. }
  68. else{
  69. insert(root1,a1,a2,lr);
  70. }
  71. }
  72. cout<<maxNodeLevel(root1)<<"";
  73. }
  74. }
  75. }
  76. /*This is a function problem.You only need to complete the function given below*/
  77. /*Complete the function below
  78. Node is as follows:
  79. struct Node{
  80. int data;
  81. Node *left,*right;
  82. };
  83. */
  84. map <int,int> result;
  85. void getResult(Node *root, int h) {
  86. if(root==NULL)
  87. return;
  88. result[h]++;
  89. getResult(root->left,h+1);
  90. getResult(root->right,h+1);
  91. }
  92. int maxNodeLevel(Node *root)
  93. {
  94. // Add your code here
  95. if(root==NULL)
  96. return 0;
  97. getResult(root, 0);
  98. int max=0,res;
  99. for(auto i=result.begin();i!=result.end();i++){
  100. if(max<i->second){
  101. max=i->second;
  102. res=i->first;
  103. }
  104. }
  105. cout<<res<<" ";
  106. result.clear();
  107. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
2
3
1 2 L 1 3 R 2 4 L
3
1 3 L 1 2 R 2 4 R
compilation info
prog.cpp:1:1: error: expected unqualified-id before ‘{’ token
 {
 ^
In file included from /usr/include/c++/8/ios:39,
                 from /usr/include/c++/8/istream:38,
                 from /usr/include/c++/8/sstream:38,
                 from /usr/include/c++/8/complex:45,
                 from /usr/include/c++/8/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/8/bits/stdc++.h:52,
                 from prog.cpp:2:
/usr/include/c++/8/exception:35:37: error: expected declaration before end of line
 #pragma GCC visibility push(default)
                                     ^
stdout
Standard output is empty