Advertisement
Rishav_hitk_cse

Untitled

Feb 2nd, 2021
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. /**
  2.  * Definition for binary tree
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8.  * };
  9.  */
  10. int ans;
  11. bool fun(TreeNode* t1,TreeNode* t2){
  12.     if(!t1)
  13.         return t2 == nullptr;
  14.     if(!t2)
  15.         return false;
  16.     if(t1->val == t2->val){
  17.         bool flg = fun(t1->left,t2->left) and fun(t1->right,t2->right);
  18.         if(!flg){
  19.             ans++;
  20.             return fun(t1->left,t2->right) and fun(t1->right,t2->left);
  21.         }else return true;
  22.     }else
  23.         return false;
  24. }
  25. int Solution::solve(TreeNode* A, TreeNode* B) {
  26.     ans = 0;
  27.     if(fun(A,B))
  28.         return ans;
  29.     return -1;
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement