Minimum Time to Burn a Binary Tree from a Leaf Node

You are given a binary tree with 'N' unique nodes, and a specific start node from where the fire will begin. The task is to determine the time in minutes required to burn the entire tree, starting from the given node.

The fire travels from the burning node to its adjacent nodes each minute, burning them down.

Input:

First line: Elements of the tree in level order form, separated by space. Use -1 for null nodes. 
Second line: Value of the start node.

Output:

An integer representing the time in minutes to burn the whole tree.

Example:

Input:
1 2 3 -1 -1 4 5 -1 -1 -1 -1
3
Output:
2
Explanation:
- Tree visualization:
1
/ \
2 3
/ \
4 5
- Start from Node 3:
0th minute: Node 3 burns.
1st minute: Nodes 1, 4, 5 burn.
2nd minute: Node 2 burns.
Entire tree burns in 2 minutes.

Constraints:

  • 1 ≤ N ≤ 105
  • 1 ≤ Value of Tree Node ≤ 109
  • 1 ≤ Value of Start Node ≤ 109
  • Time limit: 1 second
Note:
Input is handled in function, no need to print the output.
AnswerBot
2d

The task is to determine the time in minutes required to burn the entire binary tree starting from a given node.

  • Traverse the tree from the given start node to calculate the time taken to burn the enti...read more

Help your peers!
Add answer anonymously...
PhonePe 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