Given a binary tree and target node. By giving the fire to the target node and fire starts to spread in a complete tree. The task is to print the sequence of the burning nodes of a binary tree.
Rules for burning the nodes :
- Fire will spread constantly to the connected nodes only.
- Every node takes the same time to burn.
- A node burns only once.
Input : 12 / \ 13 10 / \ 14 15 / \ / \ 21 24 22 23 target node = 14 Output : 14 21, 24, 10 15, 12 22, 23, 13 Explanation : First node 14 burns then it gives fire to it's neighbors(21, 24, 10) and so on. This process continues until the whole tree burns. Input : 12 / \ 19 82 / / \ 41 15 95 \ / / \ 2 21 7 16 target node = 41 Output : 41 2, 19 12 82 15, 95 21, 7, 16
First search the target node in a binary tree recursively. After finding the target node print it and save its left child(if exist) and right child(if exist) in a queue. and return. Now, get the size of the queue and run while loop. Print elements in the queue.
Below is the implementation of the above approach :
14 21 , 22 , 13 15 , 10 23 , 24 , 12
- Get last node of the binary tree following given pattern starting from X
- Kth ancestor of a node in binary tree | Set 2
- K-th ancestor of a node in Binary Tree
- Search a node in Binary Tree
- Get Level of a node in a Binary Tree
- K-th ancestor of a node in Binary Tree | Set 3
- Get maximum left node in binary tree
- Print cousins of a given node in Binary Tree
- Find the Deepest Node in a Binary Tree
- Find mirror of a given node in Binary tree
- Replace node with depth in a binary tree
- Total sum except adjacent of a given node in a Binary Tree
- Closest leaf to a given node in Binary Tree
- Kth node in Diagonal Traversal of Binary Tree
- Inorder Successor of a node in Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.