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 it’s child nodes after removing parent node from the queue. We will continue this process until the queue empty. The last node in the queue is the deepest node of the binary tree.



Below is the implementation of the above approach :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the value of the 
// deepest node in a given binary tree.
#include <bits/stdc++.h>
using namespace std;
  
// A tree node
struct Node 
    int data; 
    struct Node *left , *right; 
}; 
  
// Utility function to create a new node
Node *newNode(int data) 
    Node *temp = new Node; 
    temp->data = data; 
    temp->left = temp->right = NULL; 
    return temp; 
  
// Function to find the deepest node in a tree
int findDeepest(struct Node* root) {
  
    struct Node* temp = NULL;
      
    queue <Node *> q;
  
    if(root == NULL)
        return 0;
      
    // Push the root node
    q.push(root);
  
    while(!q.empty()) {
  
        temp = q.front();
        q.pop();
          
        // Push left child
        if(temp->left)
            q.push(temp->left);
          
        // Push right child
        if(temp->right)
            q.push(temp->right);
    }
      
    // Return the deepest node's value
    return temp->data; 
}
  
// Driver code
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);
      
    // Function call
    cout << findDeepest(root); 
      
    return 0; 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the value of the 
// deepest node in a given binary tree.
import java.util.*;
  
class GFG 
{
  
// A tree node
static class Node 
    int data; 
    Node left , right; 
}; 
  
// Utility function to create a new node
static Node newNode(int data) 
    Node temp = new Node(); 
    temp.data = data; 
    temp.left = temp.right = null
    return temp; 
  
// Function to find the deepest node in a tree
static int findDeepest(Node root)
{
    Node temp = null;
      
    Queue <Node> q = new LinkedList<Node>();
  
    if(root == null)
        return 0;
      
    // Push the root node
    q.add(root);
  
    while(!q.isEmpty()) 
    {
        temp = q.peek();
        q.remove();
          
        // Push left child
        if(temp.left!=null)
            q.add(temp.left);
          
        // Push right child
        if(temp.right!=null)
            q.add(temp.right);
    }
      
    // Return the deepest node's value
    return temp.data; 
}
  
// Driver code
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);
      
    // Function call
    System.out.println(findDeepest(root));
}
}
  
// This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the value of the 
// deepest node in a given binary tree.
using System;
using System.Collections.Generic;
  
class GFG 
{
  
// A tree node
public class Node 
    public int data; 
    public Node left , right; 
}; 
  
// Utility function to create a new node
static Node newNode(int data) 
    Node temp = new Node(); 
    temp.data = data; 
    temp.left = temp.right = null
    return temp; 
  
// Function to find the deepest node in a tree
static int findDeepest(Node root)
{
    Node temp = null;
      
    Queue <Node> q = new Queue <Node>();
  
    if(root == null)
        return 0;
      
    // Push the root node
    q.Enqueue(root);
  
    while(q.Count != 0) 
    {
        temp = q.Peek();
        q.Dequeue();
          
        // Push left child
        if(temp.left != null)
            q.Enqueue(temp.left);
          
        // Push right child
        if(temp.right != null)
            q.Enqueue(temp.right);
    }
      
    // Return the deepest node's value
    return temp.data; 
}
  
// Driver code
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);
      
    // Function call
    Console.WriteLine(findDeepest(root));
}
}
      
// This code is contributed by PrinciRaj1992 

chevron_right



Output:

 9

Time Complexity: O(N)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : 29AjayKumar, princiraj1992



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.