Given a binary tree, target node X and a string patt. The task is to find the last node of the binary tree following the pattern starting from X. Pattern may contain only five types of character ‘p’, ‘l’, ‘r’, ‘m’ and ‘n’. For any character encountered:
- p: Get to the parent of the current node.
- l: Get to the left child of the current node.
- r: Get to the right child of the current node.
- m: Get to the left sibling in the same level.
- n: Get to the right sibling in the same level.
Note that if the node doesn’t exist corresponding to any character then skip that character and remain on the current node.
Input: 10 / \ 12 13 / \ 14 15 / \ / \ 21 22 23 24 X = 14, patt = "npmrprrlm" Output: 22 Starting from the target node 14 n: 14 -> 15 p: 15 -> 13 m: 13 -> 12 r: 12 -> 12 (there is no right child) p: 12 -> 10 r: 10 -> 13 r: 13 -> 15 l: 15 -> 23 m: 23 -> 22 Input: 5 / \ 16 8 X = 16, patt = "pppp" Output: 5
- Create an auxiliary tree from the original tree but include three extra pointers (parent pointer, left and right sibling pointers).
- Parent pointer will point to the parent of the current node.
- Left pointer will point to the left sibling.
- Right pointer will point to the right sibling.
- Check the image below:
- After creation of this tree, we can easily traverse the given pattern.
- If any node doesn’t exist corresponding any character then simply skip that character.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Burn the binary tree starting from the target node
- Print the following pyramid pattern
- Minimum time to burn a Tree starting from a Leaf node
- Delete the last leaf node in a Binary Tree
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Get Level of a node in a Binary Tree
- Get level of a node in binary tree | iterative approach
- Get maximum left node in binary tree
- Check whether a given binary tree is skewed binary tree or not?
- Print matrix in snake pattern from the last column
- Printing the Triangle Pattern using last term N
- Find parent of given node in a Binary Tree with given postorder traversal
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Check if a binary tree is subtree of another binary tree | Set 1
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 2
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Check whether a binary tree is a full binary tree or not
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Minimum swap required to convert binary tree to binary search 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.
Improved By : chsadik99