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
- Level Order Predecessor of a node in Binary Tree
- Postorder successor of a Node in Binary Tree
- Preorder Successor of a Node in Binary Tree
- Inorder Successor of a node in Binary Tree
- Replace each node in binary tree with the sum of its inorder predecessor and successor
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Get Level of a node in a Binary Tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Find the maximum node at a given level in a binary tree
- Depth of the deepest odd level node in Binary Tree
- Get level of a node in binary tree | iterative approach
- Insertion in a Binary Tree in level order
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Perfect Binary Tree Specific Level Order Traversal