Deepest right leaf node in a binary tree | Iterative approach
Last Updated :
17 Mar, 2023
Given a Binary Tree, find the deepest leaf node that is the right child of its parent. For example, consider the following tree. The deepest right leaf node is the node with the value 10.
Examples:
Input :
1
/ \
2 3
\ / \
4 5 6
\ \
7 8
/ \
9 10
Output : 10
The idea is similar to Method 2 of level order traversal
Traverse the tree level by level and while pushing right child to queue, check if it is leaf node, if it’s leaf node, then update the result and since we are traversing the level by level, the last stored right leaf will be the deepest right leaf node.
Algorithm:
- Start with the root node of the binary tree.
- Create a queue for level order traversal.
- Push the root node into the queue.
- Initialize a variable named result to NULL.
- Traverse the tree level by level using the queue until the queue is empty.
- For each node, first check if it has a left child. If yes, then push it into the queue.
- Then check if it has a right child. If yes, then push it into the queue.
- If the node has a right child and it is a leaf node (i.e., both its left and right child are NULL), then update the result variable with this node.
- Once the traversal is complete, result will hold the deepest right leaf node of the binary tree.
- Return result.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int data)
{
Node* temp = new Node();
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
Node* getDeepestRightLeafNode(Node* root)
{
if (!root)
return NULL;
queue<Node*> q;
q.push(root);
Node* result = NULL;
while (!q.empty()) {
Node* temp = q.front();
q.pop();
if (temp->left) {
q.push(temp->left);
}
if (temp->right) {
q.push(temp->right);
if (!temp->right->left && !temp->right->right)
result = temp->right;
}
}
return result;
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->right = newNode(4);
root->right->left = newNode(5);
root->right->right = newNode(6);
root->right->left->right = newNode(7);
root->right->right->right = newNode(8);
root->right->left->right->left = newNode(9);
root->right->right->right->right = newNode(10);
Node* result = getDeepestRightLeafNode(root);
if (result)
cout << "Deepest Right Leaf Node :: "
<< result->data << endl;
else
cout << "No result, right leaf not found\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node left, right;
};
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
static Node getDeepestRightLeafNode(Node root)
{
if (root == null )
return null ;
Queue<Node> q = new LinkedList<>();
q.add(root);
Node result = null ;
while (!q.isEmpty())
{
Node temp = q.peek();
q.poll();
if (temp.left != null )
{
q.add(temp.left);
}
if (temp.right != null )
{
q.add(temp.right);
if (temp.right.left == null && temp.right.right == null )
result = temp.right;
}
}
return result;
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.right = newNode( 4 );
root.right.left = newNode( 5 );
root.right.right = newNode( 6 );
root.right.left.right = newNode( 7 );
root.right.right.right = newNode( 8 );
root.right.left.right.left = newNode( 9 );
root.right.right.right.right = newNode( 10 );
Node result = getDeepestRightLeafNode(root);
if (result != null )
System.out.println( "Deepest Right Leaf Node :: "
+ result.data);
else
System.out.println( "No result, right leaf not found\n" );
}
}
|
Python3
_MIN = - 2147483648
_MAX = 2147483648
class newnode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def getDeepestRightLeafNode(root) :
if ( not root):
return None
q = []
q.append(root)
result = None
while ( len (q)):
temp = q[ 0 ]
q.pop( 0 )
if (temp.left):
q.append(temp.left)
if (temp.right):
q.append(temp.right)
if ( not temp.right.left and
not temp.right.right):
result = temp.right
return result
if __name__ = = '__main__' :
root = newnode( 1 )
root.left = newnode( 2 )
root.right = newnode( 3 )
root.left.right = newnode( 4 )
root.right.left = newnode( 5 )
root.right.right = newnode( 6 )
root.right.left.right = newnode( 7 )
root.right.right.right = newnode( 8 )
root.right.left.right.left = newnode( 9 )
root.right.right.right.right = newnode( 10 )
result = getDeepestRightLeafNode(root)
if result:
print ( "Deepest Right Leaf Node ::" ,
result.data)
else :
print ( "No result, right leaf not found" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public Node left, right;
};
static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
static Node getDeepestRightLeafNode(Node root)
{
if (root == null )
return null ;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
Node result = null ;
while (q.Count!=0)
{
Node temp = q.Peek();
q.Dequeue();
if (temp.left != null )
{
q.Enqueue(temp.left);
}
if (temp.right != null )
{
q.Enqueue(temp.right);
if (temp.right.left == null && temp.right.right == null )
result = temp.right;
}
}
return result;
}
public static void Main(String[] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.right = newNode(4);
root.right.left = newNode(5);
root.right.right = newNode(6);
root.right.left.right = newNode(7);
root.right.right.right = newNode(8);
root.right.left.right.left = newNode(9);
root.right.right.right.right = newNode(10);
Node result = getDeepestRightLeafNode(root);
if (result != null )
Console.WriteLine( "Deepest Right Leaf Node :: "
+ result.data);
else
Console.WriteLine( "No result, right leaf not found\n" );
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let temp = new Node(data);
return temp;
}
function getDeepestRightLeafNode(root)
{
if (root == null )
return null ;
let q = [];
q.push(root);
let result = null ;
while (q.length > 0)
{
let temp = q[0];
q.shift();
if (temp.left != null )
{
q.push(temp.left);
}
if (temp.right != null )
{
q.push(temp.right);
if (temp.right.left == null &&
temp.right.right == null )
result = temp.right;
}
}
return result;
}
let root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.right = newNode(4);
root.right.left = newNode(5);
root.right.right = newNode(6);
root.right.left.right = newNode(7);
root.right.right.right = newNode(8);
root.right.left.right.left = newNode(9);
root.right.right.right.right = newNode(10);
let result = getDeepestRightLeafNode(root);
if (result != null )
document.write( "Deepest Right Leaf Node :: "
+ result.data);
else
document.write( "No result, right leaf not found" );
</script>
|
Output
Deepest Right Leaf Node :: 10
Time Complexity: O(n)
Auxiliary Space: O(n) because using queue
Share your thoughts in the comments
Please Login to comment...