Find the Deepest Node in a Binary Tree Using Queue STL – SET 2
Given a binary tree. The task is to find the value of the deepÂest node in the given binary tree.
Examples:
Input: Root of below tree
1
/ \
2 3
/ \ / \
4 5 6 7
\
8
Output: 8
Input: Root of below tree
1
/ \
2 3
/
6
Output: 6
Approach: We have already discussed the two different ways to find the deepest node in a binary tree in this post. Here, we will find the deepest node in the tree using a queue data structure. We will first push the root into the queue and then we will push its child nodes after removing the parent node from the queue. We will continue this process until the queue is empty. The last node in the queue is the deepest node of the binary tree.
Below is the implementation of the above approach as follows:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
Node* newNode( int data)
{
Node* temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
int findDeepest( struct Node* root)
{
struct Node* temp = NULL;
queue<Node*> q;
if (root == NULL)
return 0;
q.push(root);
while (!q.empty()) {
temp = q.front();
q.pop();
if (temp->left)
q.push(temp->left);
if (temp->right)
q.push(temp->right);
}
return temp->data;
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = 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);
cout << findDeepest(root);
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 int findDeepest(Node root)
{
Node temp = null ;
Queue <Node> q = new LinkedList<Node>();
if (root == null )
return 0 ;
q.add(root);
while (!q.isEmpty())
{
temp = q.peek();
q.remove();
if (temp.left!= null )
q.add(temp.left);
if (temp.right!= null )
q.add(temp.right);
}
return temp.data;
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = 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 );
System.out.println(findDeepest(root));
}
}
|
Python3
from collections import deque
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def findDeepest(root: Node) - > int :
temp = None
q = deque()
if (root = = None ):
return 0
q.append(root)
while q:
temp = q[ 0 ]
q.popleft()
if (temp.left):
q.append(temp.left)
if (temp.right):
q.append(temp.right)
return temp.data
if __name__ = = "__main__" :
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.left = Node( 4 )
root.right.left = Node( 5 )
root.right.right = Node( 6 )
root.right.left.right = Node( 7 )
root.right.right.right = Node( 8 )
root.right.left.right.left = Node( 9 )
print (findDeepest(root))
|
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 int findDeepest(Node root)
{
Node temp = null ;
Queue <Node> q = new Queue <Node>();
if (root == null )
return 0;
q.Enqueue(root);
while (q.Count != 0)
{
temp = q.Peek();
q.Dequeue();
if (temp.left != null )
q.Enqueue(temp.left);
if (temp.right != null )
q.Enqueue(temp.right);
}
return temp.data;
}
public static void Main(String[] args)
{
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = 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);
Console.WriteLine(findDeepest(root));
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .left = null ;
this .right = null ;
}
};
function newNode(data)
{
var temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
function findDeepest(root)
{
var temp = null ;
var q = [];
if (root == null )
return 0;
q.push(root);
while (q.length != 0)
{
temp = q[0];
q.shift();
if (temp.left != null )
q.push(temp.left);
if (temp.right != null )
q.push(temp.right);
}
return temp.data;
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = 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);
document.write(findDeepest(root));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N) because using queue “q”
Last Updated :
08 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...