Given a BST, find the inorder predecessor of a given key in it. If the key does not lie in the BST, return the previous greater node (if any) present in the BST. Show An inorder predecessor of a node in the BST is the previous node in the inorder traversal of it. For example, consider the following tree:
A node’s inorder predecessor is a node with maximum value in its left subtree, i.e., its left subtree’s right-most child. If the left subtree of the node doesn’t exist, then the inorder predecessor is one of its ancestors. To find which ancestors are the predecessor, move up the tree towards the root until we encounter a node that is the right child of its parent. If any such node is found, then the inorder predecessor is its parent; otherwise, the inorder predecessor does not exist for the node. Recursive VersionWe can recursively check the above conditions. The idea is to search for the given node in the tree and update the predecessor to the current node before visiting its right subtree. If the node is found in the BST, return the maximum value node in its left subtree. If the left subtree of the node doesn’t exist, then the inorder predecessor is one of its ancestors, which is already being updated while searching for the given key. Following is the C++, Java, and Python implementation of the idea: C++
Download Run Code Java
Download Run Code Python
Download Run Code Output: The predecessor of node 15 is 12 The time complexity of the above solution is O(n), where Iterative VersionThe same algorithm can be easily implemented iteratively as follows in C++, Java, and Python: C++
Download Run Code Java
Download Run Code Python
Download Run Code Output: The predecessor of node 15 is 12 The time complexity of the above solution is O(n), where Thanks for reading. Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages. Like us? Refer us to your friends and help us grow. Happy coding 🙂 How do you find the successor and predecessor of a binary tree?Root is the given key: In this case, if the left subtree is not NULL, then predecessor is the rightmost node in left subtree and if right subtree is not NULL, then successor is the leftmost node in right subtree. Root is greater than key: In this case, the key is present in left subtree of root.
What is the inorder predecessor of 15 in given binary search tree?Here, 13 is 15's in-order predecessor.
Where is the inorder predecessor of a node A in a binary search tree?We'll use a recursive approach to find the inorder predecessor of a node in a binary search tree. The idea behind the approach is simple: if the left child exists for a node, then the inorder predecessor of that node will be the maximum node in the left subtree.
Does root node have predecessor?The inorder predecessor of a node p is the node q that comes just before p in the binary tree's inorder traversal. Given the root node of a binary search tree and the node p , find the inorder predecessor of node p . If it does not exist, return null .
|