Advertisement
imashutosh51

Step-by-step directions from a binary tree to another

Jul 23rd, 2022 (edited)
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.75 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
  8.  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
  9.  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
  10.  * };
  11.  */
  12.  
  13. //LOGIC:We will find LCA and then find path between LCA to start_node and LCA to end_node so start_node to LCA path will be only UP
  14. //wo we pushed in answer string only U for path length from start_node to LCA and then pushed path from LCA to end.
  15. //Time complexity:O(3N)
  16. //follow method2 for O(N)
  17. class Solution {
  18.     TreeNode* LCA(TreeNode* root,int startValue,int destValue){
  19.         if(root==NULL || root->val==startValue || root->val==destValue)
  20.             return root;
  21.        
  22.         TreeNode* l=LCA(root->left,startValue,destValue);
  23.         TreeNode* r=LCA(root->right,startValue,destValue);
  24.        
  25.         if(l==NULL)  // It means we didn't get anything in left so if left have something, return it else null.
  26.             return r;
  27.        
  28.         if(r==NULL)  //It means that l is having some because it came to this if statement by not following the conditional
  29.             return l; //statement of previous if so l have some,so return it.
  30.        
  31.         return root;  //It means l and r both have something because control came to this line by rejecting conditional
  32.                       //statement of both of the above if so it is LCS so return it.
  33.     }
  34.    
  35.     bool shortestPath(TreeNode* node,int key,string &s,char dir){
  36.         if(node==NULL){
  37.             return 0;
  38.         }
  39.        
  40.         if(node->val==key){
  41.             s.push_back(dir);
  42.             return 1;
  43.         }
  44.        
  45.         s.push_back(dir);
  46.        
  47.         if(shortestPath(node->left,key,s,'L')){
  48.             return 1;
  49.         }
  50.        
  51.         if(shortestPath(node->right,key,s,'R')){
  52.             return 1;
  53.         }
  54.         s.pop_back();   //If path is not possible with this node,then pop the current node also
  55.         return 0;
  56.     }
  57. public:
  58.     string getDirections(TreeNode* root, int startValue, int destValue) {
  59.         if(root==NULL)
  60.             return {};
  61.        
  62.         TreeNode* node=LCA(root,startValue,destValue);
  63.         string start,end;
  64.        
  65.         shortestPath(node,startValue,start,'-1');
  66.  
  67.         shortestPath(node,destValue,end,'-1');
  68.  
  69.        
  70.         if(start.size()==0)
  71.             return end;
  72.        
  73.         string ans;
  74.         for(int i=1;i<start.size();i++){  //because from left node it will be always up till the LCA
  75.             ans+='U';
  76.         }
  77.        
  78.          for(int i=1;i<end.size();i++){
  79.             ans+=end[i];
  80.         }
  81.         return ans;
  82.     }
  83. };
  84.  
  85. //Method2:
  86. //logic:we find the path from actual root to start_value then actual root to end_value and popped the path until LCA is achieved.
  87. //then start_node to lca path will be always up so added U in answer string and LCA path to end_value added in answer string.
  88. /*
  89. #include <bits/stdc++.h>
  90. using namespace std;
  91.  
  92. // Structure of Tree
  93. class TreeNode {
  94. public:
  95.     int val;
  96.     TreeNode* left;
  97.     TreeNode* right;
  98.     TreeNode(int val2)
  99.     {
  100.         val = val2;
  101.         left = NULL;
  102.         right = NULL;
  103.     }
  104. };
  105.  
  106. // Find Function
  107. bool find(TreeNode* n, int val,string& path)
  108. {
  109.     if (n->val == val)
  110.         return true;
  111.     if (n->left && find(n->left,
  112.                         val, path)) {
  113.         path.push_back('L');
  114.         return true;
  115.     }
  116.     if (n->right && find(n->right, val, path)) {
  117.         path.push_back('R');
  118.         return true;
  119.     }
  120.     return false;  //If you couldn't find the node from both left and right subtree then answer can't be with this node.
  121. }
  122.  
  123. // Function to keep track
  124. // of directions at any point
  125. string getDirections(TreeNode* root, int startValue, int destValue)
  126. {
  127.     // To store the startPath and destPath
  128.     string s_p, d_p;
  129.     find(root, startValue, s_p); //It will have path from bottom to top or startValue to top.
  130.     find(root, destValue, d_p);  //It will also have path from bottom or top to destValue to top.
  131.  
  132.     while (!s_p.empty() && !d_p.empty() && s_p.back() == d_p.back()) { //search from top till the path going same or till LCS
  133.         s_p.pop_back();
  134.         d_p.pop_back();
  135.     }
  136.  
  137.     for (int i = 0; i < s_p.size(); i++) {
  138.         s_p[i] = 'U';
  139.     }
  140.     reverse(d_p.begin(), d_p.end());
  141.     string ans = s_p + d_p;
  142.     return ans;
  143. }
  144.  
  145. // Driver Code
  146. int main()
  147. {
  148.     TreeNode* root = new TreeNode(5);
  149.     root->left = new TreeNode(1);
  150.     root->right = new TreeNode(2);
  151.     root->left->left = new TreeNode(3);
  152.     root->right->left = new TreeNode(6);
  153.     root->right->right = new TreeNode(4);
  154.  
  155.     int startValue = 3;
  156.     int endValue = 6;
  157.  
  158.     // Function Call
  159.     string ans = getDirections(root, startValue, endValue);
  160.  
  161.     // Print the result
  162.     cout << ans;
  163. }
  164. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement