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).
“””Python3 program to find preorder
predecessor of given node.”””
# A Binary Tree Node
# Utility function to create a new tree node
# Constructor to create a newNode
def __init__(self, data):
self.value = data
self.left = None
self.right = self.parent = None
def preorderPredecessor(root, n):
# Root has no predecessor in preorder
if (n == root) :
# If given node is left child of its
# parent or parent’s left is empty, then
# parent is Preorder Predecessor.
parent = n.parent
if (parent.left == None or
parent.left == n):
# In all other cases, find the rightmost
# child in left substree of parent.
curr = parent.left
while (curr.right != None) :
curr = curr.right
# Driver Code
if __name__ == ‘__main__’:
root = newNode(20)
root.parent = None
root.left = newNode(10)
root.left.parent = root
root.left.left = newNode(4)
root.left.left.parent = root.left
root.left.right = newNode(18)
root.left.right.parent = root.left
root.right = newNode(26)
root.right.parent = root
root.right.left = newNode(24)
root.right.left.parent = root.right
root.right.right = newNode(27)
root.right.right.parent = root.right
root.left.right.left = newNode(14)
root.left.right.left.parent = root.left.right
root.left.right.left.left = newNode(13)
root.left.right.left.left.parent = root.left.right.left
root.left.right.left.right = newNode(15)
root.left.right.left.right.parent = root.left.right.left
root.left.right.right = newNode(19)
root.left.right.right.parent = root.left.right
res = preorderPredecessor(root, root.left.right.right)
print(“Preorder predecessor of”,
root.left.right.right.value, “is”, res.value)
print(“Preorder predecessor of”,
root.left.right.right.value, “is None”)
# This code is contributed
# by SHUBHAMSINGH10
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.
- Level Order Predecessor of a node in Binary Tree
- Postorder predecessor of a Node in Binary Search Tree
- Replace each node in binary tree with the sum of its inorder predecessor and successor
- Preorder Successor of a Node in Binary Tree
- Find n-th node in Preorder traversal of a Binary Tree
- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Modify a binary tree to get preorder traversal using right pointers only
- Calculate depth of a full Binary tree from Preorder
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Construct Full Binary Tree from given preorder and postorder traversals
- Check if a given array can represent Preorder Traversal of Binary Search Tree
- Sum of cousins of a given node in a Binary Tree
- K-th ancestor of a node in Binary Tree | Set 3
- Search a node in Binary Tree
- Get Level of a node in a 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 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.