Minimum time taken to burn the BT from a given Node

Binary Trees FAQs Hard
  • While this specific problem about setting a binary tree on fire is not directly applied in the software industry, the underlying concepts of tree traversal, breadth-first search, depth-first search, and shortest path finding are essential in various areas
  • They are used in data mining and machine learning algorithms, routing protocols like Open Shortest Path First in networks, file system hierarchies in operating systems, graph databases, and even in the layout rendering in Android's XML design
  • This problem helps to enhance the understanding of these concepts in a unique way

Given a target node data and a root of binary tree. If the target is set on fire, determine the shortest amount of time needed to burn the entire binary tree.


It is known that in 1 second all nodes connected to a given node get burned. That is its left child, right child, and parent.

Examples:

Input : root = [1, 2, 3, 4, null, 5, 6, null, 7]. target = 1

Output : 3

Explanation : The node with value 1 is set on fire.

In 1st second it burns node 2 and node 3.

In 2nd second it burns nodes 4, 5, 6.

In 3rd second it burns node 7.

Input : root = [1, 2, 3, null, 5, null, 4], target = 4

Output : 4

Explanation : The node with value 1 is set on fire.

In 1st second it burns node 3.

In 2nd second it burns node 1.

In 3rd second it burns node 2.

In 4th second it burns node 5.

Input : root = [1, 2, 3, 6, 5, 8, 4], target = 4


Constraints

  • 1 <= Number of Nodes <= 104
  • -105 <= Node.val <= 105
  • All Node.val values are unique.

Hints

  • Use DFS (or BFS) to create an adjacency list representation of the tree. Each node should have references to its left child, right child, and parent to allow traversal in both directions.
  • Start BFS traversal from the target node (fire source). Spread fire level by level every second. Keep track of visited nodes to avoid cycles.

Company Tags

eBay Airbnb Etsy American Express Shopify Mastercard Seagate Technology HashiCorp Roche Unity Technologies Uber Visa Goldman Sachs Boston Consulting Group Instacart OYO Rooms NVIDIA Cerner Optum Nutanix Cloudflare Epic Systems Rockstar Games Docker Snowflake Google Microsoft Amazon Meta Apple Netflix Adobe