Given a binary tree and a node in the binary tree, find Preorder predecessor of the given node.
Consider the following binary tree 20 / \ 10 26 / \ / \ 4 18 24 27 / \ 14 19 / \ 13 15 Input : 4 Output : 10 Preorder traversal of given tree is 20, 10, 4, 18, 14, 13, 15, 19, 26, 24, 27. Input : 19 Output : 15
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 the given node is root, then return NULL as preorder predecessor.
- If node is the left child of its parent or left child of parent is NULL, then return parent as its preorder predecessor.
- If node is the right child of its parent and left child of parent exists, then predecessor would be the rightmost node (max value) of the left subtree of parent.
- If node is the right child of its parent and the parent has no left child, then predecessor would be the parent node (max value).
Preorder predecessor of 19 is 15
Time Complexity : O(h) where h is height of given Binary Tree
Auxiliary Space : O(1) since no use of arrays, stacks, queues.
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.
- Level Order Predecessor of a node in Binary Tree
- Check if a binary tree is subtree of another binary tree using preorder traversal : Iterative
- Preorder Successor of a Node in Binary Tree
- Find n-th node in Preorder traversal of a Binary Tree
- 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
- Check if given Preorder, Inorder and Postorder traversals are of same tree
- Iterative Preorder Traversal of an N-ary Tree
- Preorder Traversal of N-ary Tree Without Recursion
- Check if given Preorder, Inorder and Postorder traversals are of same tree | Set 2
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Check whether a given binary tree is skewed binary tree or not?
- Find postorder traversal of BST from preorder traversal
- Number of elements smaller than root using preorder traversal of a BST
- Inorder Successor of a node in Binary Tree
- Kth ancestor of a node in binary tree | Set 2
- Sort the path from root to a given node in a Binary Tree
- Sum of cousins of a given node in a Binary Tree
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
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.