imashutosh51

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

Jul 23rd, 2022 (edited)
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.04 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. def LCA(root, startValue, destValue):
  18.     if root is None or root.val == startValue or root.val == destValue:
  19.         return root
  20.    
  21.     l = LCA(root.left, startValue, destValue)
  22.     r = LCA(root.right, startValue, destValue)
  23.    
  24.     if l is None:  # It means we didn't get anything in left so if left have something, return it else null.
  25.         return r
  26.    
  27.     if r is None:  # It means that l is having some because it came to this if statement by not following the conditional
  28.         return l   # statement of previous if so l have some,so return it.
  29.    
  30.     return root   # It means l and r both have something because control came to this line by rejecting conditional
  31.                   # statement of both of the above if so it is LCS so return it.
  32.  
  33.  
  34. def shortestPath(node, key, s, dir):
  35.     if node is None:
  36.         return False
  37.    
  38.     if node.val == key:
  39.         s.append(dir)
  40.         return True
  41.    
  42.     s.append(dir)
  43.    
  44.     if shortestPath(node.left, key, s, 'L'):
  45.         return True
  46.    
  47.     if shortestPath(node.right, key, s, 'R'):
  48.         return True
  49.    
  50.     s.pop()   # If path is not possible with this node,then pop the current node also
  51.     return False
  52.  
  53.  
  54. class Solution:
  55.     def getDirections(self, root, startValue, destValue):
  56.         if root is None:
  57.             return ""
  58.        
  59.         node = LCA(root, startValue, destValue)
  60.         start = []
  61.         end = []
  62.        
  63.         shortestPath(node, startValue, start, '-')  # Python does not support multi-char char literal
  64.        
  65.         shortestPath(node, destValue, end, '-')
  66.        
  67.         if len(start) == 0:
  68.             return "".join(end)
  69.        
  70.         ans = []
  71.        
  72.         for i in range(1, len(start)):  # because from left node it will be always up till the LCA
  73.             ans.append('U')
  74.        
  75.         for i in range(1, len(end)):
  76.             ans.append(end[i])
  77.        
  78.         return "".join(ans)
  79.  
  80. //Method2:
  81. //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.
  82. //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.
  83. /*
  84. # Find Function
  85. def find(n, val, path):
  86.     if n.val == val:
  87.         return True
  88.    
  89.     if n.left and find(n.left, val, path):
  90.         path.append('L')
  91.         return True
  92.    
  93.     if n.right and find(n.right, val, path):
  94.         path.append('R')
  95.         return True
  96.    
  97.     return False  # If you couldn't find the node from both left and right subtree then answer can't be with this node.
  98.  
  99.  
  100. class Solution:
  101.    
  102.     # Function to keep track
  103.     # of directions at any point
  104.     def getDirections(self, root, startValue, destValue):
  105.        
  106.         # To store the startPath and destPath
  107.         s_p = []
  108.         d_p = []
  109.        
  110.         find(root, startValue, s_p)  # It will have path from bottom to top or startValue to top.
  111.         find(root, destValue, d_p)   # It will also have path from bottom or top to destValue to top.
  112.        
  113.         # search from top till the path going same or till LCS
  114.         while s_p and d_p and s_p[-1] == d_p[-1]:
  115.             s_p.pop()
  116.             d_p.pop()
  117.        
  118.         for i in range(len(s_p)):
  119.             s_p[i] = 'U'
  120.        
  121.         d_p.reverse()
  122.        
  123.         ans = "".join(s_p) + "".join(d_p)
  124.         return ans
Advertisement
Add Comment
Please, Sign In to add comment