# Level Order Predecessor of a node in Binary Tree

Given a binary tree and a node in the binary tree, find Levelorder Predecessor of the given node. That is, the node that appears before 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.

Examples:

```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 : 19
Output : 14

Input : 4
Output : 26
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

1. Check if the root is NULL, that is tree is empty. If true then return NULL.
2. Check if the given node is root. If true than there will be no predecessor of root, so return NULL.
3. Otherwise, perform Level Order Traversal on the tree using a Queue and a temporary pointer prev to maintain the last node during the traversal.
4. At every step of the level order traversal, check if the current node matches with the given node.
5. If True, stop traversing any further and return the node stored in the pointer prev as it is the node accessed just before the current node during the traversal.

Below is the implementation of the above approach:

## C++

 `// CPP program to find Levelorder ` `// Predecessor of given node in the ` `// Binary Tree ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Tree Node ` `struct` `Node { ` `    ``struct` `Node *left, *right; ` `    ``int` `value; ` `}; ` ` `  `// Utility function to create a ` `// new node with given value ` `struct` `Node* newNode(``int` `value) ` `{ ` `    ``Node* temp = ``new` `Node; ` `    ``temp->left = temp->right = NULL; ` `    ``temp->value = value; ` ` `  `    ``return` `temp; ` `} ` ` `  `// Function to find the Level Order Predecessor ` `// of a given Node in Binary Tree ` `Node* levelOrderPredecessor(Node* root, Node* key) ` `{ ` `    ``// Base Case ` `    ``if` `(root == NULL) ` `        ``return` `NULL; ` ` `  `    ``// If root equals to key ` `    ``if` `(root == key) { ` ` `  `        ``// There is no Predecessor of ` `        ``// root node ` `        ``return` `NULL; ` `    ``} ` ` `  `    ``// Create an empty queue for level ` `    ``// order traversal ` `    ``queue q; ` ` `  `    ``// Enqueue Root ` `    ``q.push(root); ` ` `  `    ``// Temporary node to keep track of the ` `    ``// last node ` `    ``Node* prev = NULL; ` ` `  `    ``while` `(!q.empty()) { ` `        ``Node* nd = q.front(); ` `        ``q.pop(); ` ` `  `        ``if` `(nd == key) ` `            ``break``; ` `        ``else` `            ``prev = nd; ` ` `  `        ``if` `(nd->left != NULL) { ` `            ``q.push(nd->left); ` `        ``} ` ` `  `        ``if` `(nd->right != NULL) { ` `            ``q.push(nd->right); ` `        ``} ` `    ``} ` ` `  `    ``return` `prev; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``struct` `Node* root = newNode(20); ` `    ``root->left = newNode(10); ` `    ``root->left->left = newNode(4); ` `    ``root->left->right = newNode(18); ` `    ``root->right = newNode(26); ` `    ``root->right->left = newNode(24); ` `    ``root->right->right = newNode(27); ` `    ``root->left->right->left = newNode(14); ` `    ``root->left->right->left->left = newNode(13); ` `    ``root->left->right->left->right = newNode(15); ` `    ``root->left->right->right = newNode(19); ` ` `  `    ``struct` `Node* key = root->left->right->right; ` ` `  `    ``struct` `Node* res = levelOrderPredecessor(root, key); ` ` `  `    ``if` `(res) ` `        ``cout << ``"LevelOrder Predecessor of "` `<< key->value ` `             ``<< ``" is "` `<< res->value; ` `    ``else` `        ``cout << ``"LevelOrder Predecessor of "` `<< key->value ` `             ``<< ``" is "` `             ``<< ``"NULL"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find Levelorder  ` `// Predecessor of given node in the  ` `// Binary Tree  ` `import` `java.util.*; ` `class` `GfG {  ` ` `  `// Tree Node  ` `static` `class` `Node {  ` `    ``Node left, right;  ` `    ``int` `value;  ` `}  ` ` `  `// Utility function to create a  ` `// new node with given value  ` `static` `Node newNode(``int` `value)  ` `{  ` `    ``Node temp = ``new` `Node();  ` `    ``temp.left = ``null``; ` `    ``temp.right = ``null``;  ` `    ``temp.value = value;  ` ` `  `    ``return` `temp;  ` `}  ` ` `  `// Function to find the Level Order Predecessor  ` `// of a given Node in Binary Tree  ` `static` `Node levelOrderPredecessor(Node root, Node key)  ` `{  ` `    ``// Base Case  ` `    ``if` `(root == ``null``)  ` `        ``return` `null``;  ` ` `  `    ``// If root equals to key  ` `    ``if` `(root == key) {  ` ` `  `        ``// There is no Predecessor of  ` `        ``// root node  ` `        ``return` `null``;  ` `    ``}  ` ` `  `    ``// Create an empty queue for level  ` `    ``// order traversal  ` `    ``Queue q = ``new` `LinkedList ();  ` ` `  `    ``// Enqueue Root  ` `    ``q.add(root);  ` ` `  `    ``// Temporary node to keep track of the  ` `    ``// last node  ` `    ``Node prev = ``null``;  ` ` `  `    ``while` `(!q.isEmpty()) {  ` `        ``Node nd = q.peek();  ` `        ``q.remove();  ` ` `  `        ``if` `(nd == key)  ` `            ``break``;  ` `        ``else` `            ``prev = nd;  ` ` `  `        ``if` `(nd.left != ``null``) {  ` `            ``q.add(nd.left);  ` `        ``}  ` ` `  `        ``if` `(nd.right != ``null``) {  ` `            ``q.add(nd.right);  ` `        ``}  ` `    ``}  ` ` `  `    ``return` `prev;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``Node root = newNode(``20``);  ` `    ``root.left = newNode(``10``);  ` `    ``root.left.left = newNode(``4``);  ` `    ``root.left.right = newNode(``18``);  ` `    ``root.right = newNode(``26``);  ` `    ``root.right.left = newNode(``24``);  ` `    ``root.right.right = newNode(``27``);  ` `    ``root.left.right.left = newNode(``14``);  ` `    ``root.left.right.left.left = newNode(``13``);  ` `    ``root.left.right.left.right = newNode(``15``);  ` `    ``root.left.right.right = newNode(``19``);  ` ` `  `    ``Node key = root.left.right.right;  ` ` `  `    ``Node res = levelOrderPredecessor(root, key);  ` ` `  `    ``if` `(res != ``null``)  ` `        ``System.out.println(``"LevelOrder Predecessor of "` `+ key.value + ``" is "` `+ res.value);  ` `    ``else` `        ``System.out.println(``"LevelOrder Predecessor of "` `+ key.value+ ``" is null"``);  ` ` `  `} ` `}  `

## C#

 `// C# program to find Levelorder  ` `// Predecessor of given node in the  ` `// Binary Tree ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `class` `GfG ` `{  ` ` `  `    ``// Tree Node  ` `    ``class` `Node  ` `    ``{  ` `        ``public` `Node left, right;  ` `        ``public` `int` `value;  ` `    ``}  ` ` `  `    ``// Utility function to create a  ` `    ``// new node with given value  ` `    ``static` `Node newNode(``int` `value)  ` `    ``{  ` `        ``Node temp = ``new` `Node();  ` `        ``temp.left = ``null``; ` `        ``temp.right = ``null``;  ` `        ``temp.value = value;  ` ` `  `        ``return` `temp;  ` `    ``}  ` ` `  `    ``// Function to find the Level Order Predecessor  ` `    ``// of a given Node in Binary Tree  ` `    ``static` `Node levelOrderPredecessor(Node root, Node key)  ` `    ``{  ` `        ``// Base Case  ` `        ``if` `(root == ``null``)  ` `            ``return` `null``;  ` ` `  `        ``// If root equals to key  ` `        ``if` `(root == key)  ` `        ``{  ` ` `  `            ``// There is no Predecessor of  ` `            ``// root node  ` `            ``return` `null``;  ` `        ``}  ` ` `  `        ``// Create an empty queue for level  ` `        ``// order traversal  ` `        ``Queue q = ``new` `Queue ();  ` ` `  `        ``// Enqueue Root  ` `        ``q.Enqueue(root);  ` ` `  `        ``// Temporary node to keep track of the  ` `        ``// last node  ` `        ``Node prev = ``null``;  ` ` `  `        ``while` `(q.Count!=0)  ` `        ``{  ` `            ``Node nd = q.Peek();  ` `            ``q.Dequeue();  ` ` `  `            ``if` `(nd == key)  ` `                ``break``;  ` `            ``else` `                ``prev = nd;  ` ` `  `            ``if` `(nd.left != ``null``)  ` `            ``{  ` `                ``q.Enqueue(nd.left);  ` `            ``}  ` ` `  `            ``if` `(nd.right != ``null``)  ` `            ``{  ` `                ``q.Enqueue(nd.right);  ` `            ``}  ` `        ``}  ` ` `  `        ``return` `prev;  ` `    ``}  ` ` `  `    ``// Driver code  ` `    ``public` `static` `void` `Main(String[] args)  ` `    ``{  ` `        ``Node root = newNode(20);  ` `        ``root.left = newNode(10);  ` `        ``root.left.left = newNode(4);  ` `        ``root.left.right = newNode(18);  ` `        ``root.right = newNode(26);  ` `        ``root.right.left = newNode(24);  ` `        ``root.right.right = newNode(27);  ` `        ``root.left.right.left = newNode(14);  ` `        ``root.left.right.left.left = newNode(13);  ` `        ``root.left.right.left.right = newNode(15);  ` `        ``root.left.right.right = newNode(19);  ` ` `  `        ``Node key = root.left.right.right;  ` ` `  `        ``Node res = levelOrderPredecessor(root, key);  ` ` `  `        ``if` `(res != ``null``)  ` `            ``Console.WriteLine(``"LevelOrder Predecessor of "` `+ ` `                                ``key.value + ``" is "` `+ res.value);  ` `        ``else` `            ``Console.WriteLine(``"LevelOrder Predecessor of "` `+ ` `                                ``key.value+ ``" is null"``);  ` `    ``} ` `}  ` ` `  `// This code is contributed by PrinciRaj1992 `

Output:

```LevelOrder Predecessor of 19 is 14
```

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.