Minimum time to burn a Tree starting from a Leaf node

As a budding programmer, you can’t really escape one of the most crucial subjects i.e binary tree!

A binary tree is defined as the tree data structure where each of the nodes have at least two children where both the children are referred to as the left children and the right children.

From tree traversal problems to burning tree problems to binary heaps, the concept is broad and vital. These tree related concepts are much asked in coding and programming related interviews.

Keeping the vitality of this topic in consideration, we have curated a blog post for one of the commonly asked tree questions i.e minimum time required to burn a tree from its leaf node.

Be on the subject and learn in-depth about the burn tree problem in depth!

What is a burning tree problem?

You often encounter the problem of burning a binary tree. Well, to burn a binary tree, you have to start from its target node. You are always given with a binary tree and the target node.

By giving fire to the target node, the fire will then spread to the complete tree. Your task would be to burn the sequence of your nodes. Then, the fire will spread to all the connected nodes. In this way, you can get a burn tree problem.

Minimum time required to burn a tree starting from its node

Now you got an idea of what a burning tree problem is, let’s come to the main part i.e time needed to burn a binary tree.

You are given a binary tree of N number of unique nodes.. You have to start from the node where the tree will start to burn. Given that start node will always exist in your tree. Your main task is to print  the time [ especially in minutes] that how much time it will take to burn the tree.

It is already given to you that the time it takes for the fire to start travelling is one minute from the burning node to the adjacent nodes and then again from the burning nodes to all other adjacent nodes.

For example:

A binary tree is given to you.

[ 1, 2, 3, -1, -1 , , 5, -1, -1, -1, -1]

Start from the node 3

 

1

 

/    \

 

2    3

 

      /       \

 

 

   4          5

 

The output you are getting is 2

Explanation

In the zero minute, your node 3 will first start burning. After passing of one minute, nodes 1, 4 and 5 which are adjacent to the node 3 will burn completely. Then after two minutes, the leftover node 2 will burn as there will be no other node left in your binary tree.

This means that your entire tree will burn in just two minutes.

Input format

The first line here contains all the elements of a tree in the form of level order. This line contains the value of all the nodes separated by the single space. Here, the node will be null and we will take -1 in its place.

The second line contains all the elements of the starting node.

1

 

/      \

 

2      3

/         \       /

 

4         5     6

 

\

 

7

 

 

For example; in the above tree, the input will be depicted as;

 

1

 

2  3

 

4   -1  5   6

 

-1    7  -1 -1 -1 -1

 

-1 -1

 

For level 1

 

The root node for this tree would be 1

 

For level 2

 

The left child for 1 would be 2

 

The right child for 1 would be 3

 

For level 3

 

Left child of 2 will be 4

 

Right child of 2 is null [-1]

 

Left child of 3 would be 5

 

Right child of 3 would be 6

 

For level 4

 

Left child of  would be null [-1]

 

Right child of 4 will be 7

 

Left child of 5 would be null [ -1]

 

Right child of 5 is null [ -1]

 

Left child of 6 would be null [-1]

 

Right child of 6 would be null [-1]

 

For level 5

 

Left child of 7 would be null -1

 

Right child of 7 would be null -1

 

 

Explanation for this:

 

The above format helped you in giving clarity on how input for a given tree will be formed.

 

The sequence  for this will then be put together in the single line separated by a space. For the above tree, the required input would come as

 

1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1 -1

 

The output format

Here, print a single integer which denotes time in minutes that will take to burn the entire tree.

Noteworthy thing here is;

 

Here, you don’t need to print anything as you only have to take care of the instructions to implement the given function.

 

We can also use an ideal approach for our burn tree to store extra information in every node. Here, you will have to consider certain things:

 

Depth of your left subtree
Depth of your right subtree
Time needed for the fire to reach your tree’s front node starting from your first leaf node that is burned
The boolean variable is used to check initial burnt node in a tree which is rooted under your current node

 

Hence, to get the minimum time required to burn a tree needs to be calculated you are left with and you want to make.

 

 

Sort a Stack

 

Another important programming concept besides the burning tree concept that you need to consider is how to sort a stack. In programming, sack is an abstract data type which helps to store elements.

 

In order to sort a stack you have to create a sack and push all the elements in it either in an ascending or the descending order.

 

 

Wrapping Up

 

Minimum time required to burn a tree can be best calculated with the help of above-mentioned approach.

 

We hope that now you will get a better idea on how to calculate the minimum time needed to burn a tree.

 

Leave a Comment

Exit mobile version