Time to Burn Tree Problem

You are given a binary tree consisting of 'N' unique nodes and a start node where the burning will commence. The task is to calculate the time in minutes required to completely burn the entire tree, given that the starting node is guaranteed to be present in the tree.

Every minute, the fire will spread from a burning node to its adjacent nodes, causing them to catch fire.

Input:

The first line contains the elements of the tree in level order form, separated by spaces, where a null node is represented by -1.
The second line contains the value of the start node.

Output:

A single integer representing the time in minutes necessary to burn the entire tree.

Example:

Input:
1 2 3 -1 -1 4 5 -1 -1 -1 -1 
3
Output:
2
Explanation:
Initial state: 
1
/ \
2 3
/ \
4 5

Minute 0: Node 3 starts burning.
Minute 1: Nodes 1, 4, and 5 catch fire.
Minute 2: Node 2 burns. The entire tree is burnt by this time.

Constraints:

  • 1 <= N <= 10^5
  • 1 <= ext{Value of Tree Node} <= 10^9
  • 1 <= ext{Value of Start Node} <= 10^9
  • Time limit: 1 second
Be the first one to answer
Add answer anonymously...
Zomato Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter