imashutosh51

Burning Tree

Jul 31st, 2022 (edited)
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. '''
  2. Logic:We can do this while BFS traversal,so we can go down but we can't go up in tree so
  3. we created a map in which we will store parent address and do bfs
  4. similary created a visited map,which will take care of viisted nodes.
  5. '''
  6. '''
  7. class Node:
  8.  
  9.    def __init__(self, val):
  10.        self.data = val
  11.        self.left = None
  12.        self.right = None
  13. '''
  14. from collections import deque
  15. def dfs(node, parent_map, target):
  16.     if not node:
  17.         return None
  18.  
  19.     target_node = None #it not found in left or right calls, target_node will return None
  20.     if node.data == target:
  21.         target_node = node
  22.        
  23.     if node.left:
  24.         parent_map[node.left] = node
  25.         left_target = dfs(node.left, parent_map, target)
  26.         if left_target:
  27.             target_node = left_target
  28.  
  29.     if node.right:
  30.         parent_map[node.right] = node
  31.         right_target = dfs(node.right, parent_map, target)
  32.  
  33.         if right_target:
  34.             target_node = right_target
  35.  
  36.     return target_node
  37.  
  38. class Solution:
  39.     def minTime(self, root, target):
  40.         parent_map = {}
  41.         target_node = dfs(root,parent_map,target)
  42.         if not target_node:
  43.             return 0
  44.         q = deque([target_node])
  45.         visited = {target_node}
  46.         time = -1
  47.  
  48.         while q:
  49.             for _ in range(len(q)):
  50.                 node = q.popleft()
  51.                 neighbors = [node.left,node.right,parent_map.get(node)]
  52.                 for neigh in neighbors:
  53.                     if neigh and neigh not in visited:
  54.                         visited.add(neigh)
  55.                         q.append(neigh)
  56.             time += 1
  57.         return time
  58.  
  59. '''
  60. Method 2:we can do this in single travesal by levelordertraversal storing parents in map
  61. and when we acheived target we will start bfs algorithm from there.because we won't need
  62. the parent of lower level nodes than the target level node.
  63. '''
  64. def dfs(node, target, ans):
  65.  
  66.     if not node:
  67.         return -1, 0
  68.  
  69.     # Recursively get:
  70.     # distance from target
  71.     # height of subtree
  72.  
  73.     left_dist, left_height = dfs(node.left, target, ans)
  74.     right_dist, right_height = dfs(node.right, target, ans)
  75.  
  76.     # Current subtree height
  77.     curr_height = 1 + max(left_height, right_height)
  78.  
  79.     # If current node is target
  80.     if node.data == target:
  81.  
  82.         # Burn time downward
  83.         ans[0] = max(ans[0], max(left_height, right_height))
  84.  
  85.         return 1, curr_height
  86.  
  87.     # Target found in left subtree
  88.     if left_dist != -1:
  89.  
  90.         # Fire reaches current node after left_dist seconds
  91.         # Then burns right subtree
  92.         ans[0] = max(ans[0], left_dist + right_height)
  93.  
  94.         return left_dist + 1, curr_height
  95.  
  96.     # Target found in right subtree
  97.     if right_dist != -1:
  98.  
  99.         ans[0] = max(ans[0], right_dist + left_height)
  100.  
  101.         return right_dist + 1, curr_height
  102.  
  103.     # Target not found
  104.     return -1, curr_height
  105.  
  106.  
  107. class Solution:
  108.  
  109.     def minTime(self, root, target):
  110.  
  111.         ans = [0]
  112.  
  113.         dfs(root, target, ans)
  114.  
  115.         return ans[0]
  116.  
Advertisement
Add Comment
Please, Sign In to add comment