Given a binary tree and a node in the binary tree, find Levelorder successor of the given node. That is, the node that appears after the given node in the level order traversal of the tree.
Note: The task is not just to print the data of the node, you have to return the complete node from the tree.
Consider the following binary tree 20 / \ 10 26 / \ / \ 4 18 24 27 / \ 14 19 / \ 13 15 Levelorder traversal of given tree is: 20, 10, 26, 4, 18, 24, 27, 14, 19, 13, 15 Input : 24 Output : 27 Input : 4 Output : 18
- Check if the root is NULL, that is tree is empty. If true then return NULL.
- Check if the given node is root. If true:
- Check if left child of root exists, if true return left child of root.
- Else, check if right child exists, return it.
- If the root is the only node. Return NULL.
- Otherwise, perform Level Order Traversal on the tree using a Queue.
- At every step of the level order traversal, check if the current node matches with the given node.
- If True, stop traversing any further and return the element at top of queue which will be the next node in the level order traversal.
Below is the implementation of the above approach:
LevelOrder successor of 24 is 27
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.
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Replace each node in binary tree with the sum of its inorder predecessor and successor
- Preorder Successor of a Node in Binary Tree
- Inorder Successor of a node in Binary Tree
- Postorder successor of a Node in Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Flatten Binary Tree in order of Level Order Traversal
- Level Order Predecessor of a node in Binary Tree
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Insertion in n-ary tree in given order and Level order traversal
- Difference between sums of odd level and even level nodes of a Binary Tree
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Connect Nodes at same Level (Level Order Traversal)
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Print extreme nodes of each level of Binary Tree in alternate order
- Print odd positioned nodes of odd levels in level order of the given 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.