Given a binary tree and a node in the binary tree, find preorder successor of the given node. It may be assumed that every node has parent link.
Consider the following binary tree 20 / \ 10 26 / \ / \ 4 18 24 27 / \ 14 19 / \ 13 15 Input : 4 Output : 18 Preorder traversal of given tree is 20, 10, 4, 18, 14, 13, 15, 19, 26, 24, 27. Input : 19 Output : 26
A simple solution is to first store Preorder traversal of the given tree in an array then linearly search given node and print node next to it.
Time Complexity : O(n)
Auxiliary Space : O(n)
An efficient solution is based on below observations.
- If left child of given node exists, then the left child is preorder successor.
- If left child does not exist and given node is left child of its parent, then its sibling is its preorder successor.
- If none of above conditions are satisfied (left child does not exist and given node is not left child of its parent), then we move up using parent pointers until one of the following happens.
- We reach root. In this case, preorder successor does not exist.
- Current node (one of the ancestors of given node) is left child of its parent, in this case preorder successor is sibling of current node.
Preorder successor of 19 is 26
Time Complexity : O(h) where h is height of given Binary Tree
Auxiliary Space : O(1)
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.
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Replace each node in binary tree with the sum of its inorder predecessor and successor
- Inorder Successor of a node in Binary Tree
- Postorder successor of a Node in Binary Tree
- Level Order Successor of a node in Binary Tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Preorder predecessor of a Node in Binary Tree
- Find n-th node in Preorder traversal of a Binary Tree
- Check if a given array can represent Preorder Traversal of Binary Search Tree
- Calculate depth of a full Binary tree from Preorder
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Modify a binary tree to get preorder traversal using right pointers only
- Construct Full Binary Tree from given preorder and postorder traversals
- Find Leftmost and Rightmost node of BST from its given preorder traversal
- Tree Traversals (Inorder, Preorder and Postorder)
- Construct Tree from given Inorder and Preorder traversals
- Construct a special tree from given preorder traversal
- Construct the full k-ary tree from its preorder traversal
- Iterative Preorder Traversal of an N-ary Tree
- Check if given Preorder, Inorder and Postorder traversals are of same tree | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.