Guest User

Untitled

a guest
May 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<queue>
  4. #include<string>
  5. using namespace std;
  6.  
  7. class node{
  8. public:
  9. string data;
  10. node*left;
  11. node*right;
  12. node(string d){
  13. data = d;
  14. left = NULL;
  15. right = NULL;
  16. }
  17. };
  18. //preorder
  19. node* buildTree(){
  20. string d;
  21. cin>>d;
  22. if(d=="false"){
  23. return NULL;
  24. }
  25. if(d=="true"){
  26. cin>>d;
  27. }
  28.  
  29. node* n = new node(d);
  30. n->left = buildTree();
  31. n->right = buildTree();
  32. return n;
  33. }
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42. int height(node*root){
  43. if(root==NULL){
  44. return 0;
  45. }
  46. int lh = height(root->left);
  47. int rh = height(root->right);
  48. return max(lh,rh)+1;
  49.  
  50. }
  51.  
  52.  
  53.  
  54.  
  55.  
  56. void preorder(node*root,vector<char>&v){
  57. if(root==NULL){
  58. v.push_back('n');
  59. return;
  60. }
  61. v.push_back('y');
  62. preorder(root->left,v);
  63. preorder(root->right,v);
  64. }
  65.  
  66. int main(){
  67. node*root = NULL;
  68. node*root1 = NULL;
  69. root = buildTree();
  70. root1 = buildTree();
  71. vector<char> v;
  72. vector<char> v1;
  73. preorder(root,v);
  74. preorder(root1,v1);
  75. int x = height(root);
  76. int y = height(root1);
  77. if(x!=y){
  78. cout<<"false";
  79. }
  80. else if(x==y){
  81. if(v.size()!=v1.size()){
  82. cout<<"false";
  83. }
  84. else{
  85. for(int i=0;i<v.size();i++){
  86. if(v[i]!=v1[i]){
  87. cout<<"false";
  88. break;
  89. }
  90.  
  91. }
  92. cout<<"true";
  93. }
  94.  
  95. }
  96.  
  97.  
  98.  
  99.  
  100. }
Add Comment
Please, Sign In to add comment