Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Tree
  • Practice Tree
  • MCQs on Tree
  • Tutorial on Tree
  • Types of Trees
  • Basic operations
  • Tree Traversal
  • Binary Tree
  • Complete Binary Tree
  • Ternary Tree
  • Binary Search Tree
  • Red-Black Tree
  • AVL Tree
  • Full Binary Tree
  • B-Tree
  • Advantages & Disadvantages
Open In App
Next Article:
Find Minimum Depth of a Binary Tree
Next article icon

Find the Deepest Node in a Binary Tree

Last Updated : 15 Feb, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a binary tree, find the deep­est node in it.

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

Method 1: The idea is to do Inorder traversal of a given binary tree. While doing Inorder traversal, we pass level of current node also. We keep track of the maximum level seen so far and the value of the deepest node seen so far. 

Implementation:

C++




// A C++ program to find 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;
}
 
// maxLevel : keeps track of maximum level seen so far.
// res :  Value of deepest node so far.
// level : Level of root
void find(Node *root, int level, int &maxLevel, int &res)
{
    if (root != NULL)
    {
        find(root->left, ++level, maxLevel, res);
 
        // Update level and rescue
        if (level > maxLevel)
        {
            res = root->data;
            maxLevel = level;
        }
 
        find(root->right, level, maxLevel, res);
    }
}
 
// Returns value of deepest node
int deepestNode(Node *root)
{
    // Initialize result and max level
    int res = -1;
    int maxLevel = -1;
 
    // Updates value "res" and "maxLevel"
    // Note that res and maxLen are passed
    // by reference.
    find(root, 0, maxLevel, res);
    return res;
}
 
// Driver program
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 << deepestNode(root);
    return 0;
}
 
 

Java




// Java program to find value of the deepest node
// in a given binary tree
class GFG
{
 
    // A tree node
    static class Node
    {
 
        int data;
        Node left, right;
 
        Node(int key)
        {
            data = key;
            left = null;
            right = null;
        }
    }
    static int maxLevel = -1;
    static int res = -1;
 
    // maxLevel : keeps track of maximum level seen so far.
    // res : Value of deepest node so far.
    // level : Level of root
    static void find(Node root, int level)
    {
        if (root != null)
        {
            find(root.left, ++level);
 
            // Update level and rescue
            if (level > maxLevel)
            {
                res = root.data;
                maxLevel = level;
            }
 
            find(root.right, level);
        }
    }
 
    // Returns value of deepest node
    static int deepestNode(Node root)
    {
        // Initialize result and max level
        /* int res = -1;
        int maxLevel = -1; */
 
        // Updates value "res" and "maxLevel"
        // Note that res and maxLen are passed
        // by reference.
        find(root, 0);
        return res;
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.right.left = new Node(5);
        root.right.right = new Node(6);
        root.right.left.right = new Node(7);
        root.right.right.right = new Node(8);
        root.right.left.right.left = new Node(9);
        System.out.println(deepestNode(root));
    }
}
 
// This code is contributed by Princi Singh
 
 

Python3




"""Python3 program to find value of the
deepest node in a given binary tree"""
 
# A Binary Tree Node
# Utility function to create a
# new tree node
class newNode:
 
    # Constructor to create a newNode
    def __init__(self, data):
        self.data= data
        self.left = None
        self.right = None
        self.visited = False
 
# maxLevel : keeps track of maximum
# level seen so far.
# res : Value of deepest node so far.
# level : Level of root
def find(root, level, maxLevel, res):
 
    if (root != None):
        level += 1
        find(root.left, level, maxLevel, res)
 
        # Update level and rescue
        if (level > maxLevel[0]):
         
            res[0] = root.data
            maxLevel[0] = level
         
        find(root.right, level, maxLevel, res)
         
# Returns value of deepest node
def deepestNode(root) :
 
    # Initialize result and max level
    res = [-1]
    maxLevel = [-1]
 
    # Updates value "res" and "maxLevel"
    # Note that res and maxLen are passed
    # by reference.
    find(root, 0, maxLevel, res)
    return res[0]
                         
# Driver Code
if __name__ == '__main__':
    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)
    print(deepestNode(root))
 
# This code is contributed by
# SHUBHAMSINGH10
 
 

C#




// C# program to find value of the deepest node
// in a given binary tree
using System;
     
class GFG
{
 
    // A tree node
    public class Node
    {
 
        public int data;
        public Node left, right;
 
        public Node(int key)
        {
            data = key;
            left = null;
            right = null;
        }
    }
    static int maxLevel = -1;
    static int res = -1;
 
    // maxLevel : keeps track of maximum level seen so far.
    // res : Value of deepest node so far.
    // level : Level of root
    static void find(Node root, int level)
    {
        if (root != null)
        {
            find(root.left, ++level);
 
            // Update level and rescue
            if (level > maxLevel)
            {
                res = root.data;
                maxLevel = level;
            }
 
            find(root.right, level);
        }
    }
 
    // Returns value of deepest node
    static int deepestNode(Node root)
    {
        // Initialize result and max level
        /* int res = -1;
        int maxLevel = -1; */
 
        // Updates value "res" and "maxLevel"
        // Note that res and maxLen are passed
        // by reference.
        find(root, 0);
        return res;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
 
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.right.left = new Node(5);
        root.right.right = new Node(6);
        root.right.left.right = new Node(7);
        root.right.right.right = new Node(8);
        root.right.left.right.left = new Node(9);
        Console.WriteLine(deepestNode(root));
    }
}
 
// This code is contributed by 29AjayKumar
 
 

Javascript




<script>
 
// JavaScript program to find value of the deepest node
// in a given binary tree
 
class Node
{
    constructor(key)
    {
        this.data = key;
            this.left = null;
            this.right = null;
    }
}
 
let maxLevel = -1;
let res = -1;
 
// maxLevel : keeps track of maximum level seen so far.
    // res : Value of deepest node so far.
    // level : Level of root
function find(root,level)
{
    if (root != null)
        {
            find(root.left, ++level);
  
            // Update level and rescue
            if (level > maxLevel)
            {
                res = root.data;
                maxLevel = level;
            }
  
            find(root.right, level);
        }
}
 
// Returns value of deepest node
function deepestNode(root)
{
    // Initialize result and max level
        /* int res = -1;
        int maxLevel = -1; */
  
        // Updates value "res" and "maxLevel"
        // Note that res and maxLen are passed
        // by reference.
        find(root, 0);
        return res;
}
 
// Driver code
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.right = new Node(6);
root.right.left.right = new Node(7);
root.right.right.right = new Node(8);
root.right.left.right.left = new Node(9);
document.write(deepestNode(root));
 
 
// This code is contributed by rag2127
 
</script>
 
 
Output
9

Time Complexity: O(n)

Auxiliary Space: O(n) for call stack

Method 2: The idea here is to find the height of the given tree and then print the node at the bottom-most level. 

Implementation:

C++




// A C++ program to find value of the
// deepest node in a given binary tree
#include <bits/stdc++.h>
using namespace std;
 
// A tree node with constructor
class Node
{
public:
    int data;
    Node *left, *right;
     
    // constructor   
    Node(int key)
    {
        data = key;
        left = NULL;
        right = NULL;
    }
};
 
// Utility function to find height
// of a tree, rooted at 'root'.
int height(Node* root)
{
  if(!root) return 0;
   
  int leftHt = height(root->left);
  int rightHt = height(root->right);
   
  return max(leftHt, rightHt) + 1;
}
 
// levels : current Level
// Utility function to print all
// nodes at a given level.
void deepestNode(Node* root, int levels)
{
    if(!root) return;
     
    if(levels == 1)
    cout << root->data;
     
    else if(levels > 1)
    {
        deepestNode(root->left, levels - 1);
        deepestNode(root->right, levels - 1);
    }
}
 
// Driver program
int main()
{
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->right->left = new Node(5);
    root->right->right = new Node(6);
    root->right->left->right = new Node(7);
    root->right->right->right = new Node(8);
    root->right->left->right->left = new Node(9);
     
    // Calculating height of tree
    int levels = height(root);
     
    // Printing the deepest node
    deepestNode(root, levels);
     
    return 0;
}
 
// This code is contributed by decode2207.
 
 

Java




// A Java program to find value of the
// deepest node in a given binary tree
class GFG
{
     
// A tree node with constructor
static class Node
{
    int data;
    Node left, right;
     
    // constructor
    Node(int key)
    {
        data = key;
        left = null;
        right = null;
    }
};
 
// Utility function to find height
// of a tree, rooted at 'root'.
static int height(Node root)
{
    if(root == null) return 0;
         
    int leftHt = height(root.left);
    int rightHt = height(root.right);
         
    return Math.max(leftHt, rightHt) + 1;
}
 
// levels : current Level
// Utility function to print all
// nodes at a given level.
static void deepestNode(Node root,
                        int levels)
{
    if(root == null) return;
     
    if(levels == 1)
    System.out.print(root.data + " ");
     
    else if(levels > 1)
    {
        deepestNode(root.left, levels - 1);
        deepestNode(root.right, levels - 1);
    }
}
 
// Driver Codede
public static void main(String args[])
{
    Node root = new Node(1);
    root.left = new Node(2);
    root.right = new Node(3);
    root.left.left = new Node(4);
    root.right.left = new Node(5);
    root.right.right = new Node(6);
    root.right.left.right = new Node(7);
    root.right.right.right = new Node(8);
    root.right.left.right.left = new Node(9);
     
    // Calculating height of tree
    int levels = height(root);
     
    // Printing the deepest node
    deepestNode(root, levels);
}
}
 
// This code is contributed by Arnab Kundu
 
 

Python3




# A Python3 program to find value of the
# deepest node in a given binary tree
class new_Node:
    def __init__(self, key):
        self.data = key
        self.left = self.right = None
         
# Utility function to find height
# of a tree, rooted at 'root'.
def height(root):
    if(not root):
        return 0
     
    leftHt = height(root.left)
    rightHt = height(root.right)
     
    return max(leftHt, rightHt) + 1
 
# levels : current Level
# Utility function to print all
# nodes at a given level.
def deepestNode(root, levels):
    if(not root):
        return
     
    if(levels == 1):
        print(root.data)
    elif(levels > 1):
        deepestNode(root.left, levels - 1)
        deepestNode(root.right, levels - 1)
 
# Driver Code
if __name__ == '__main__':
 
    root = new_Node(1)
    root.left = new_Node(2)
    root.right = new_Node(3)
    root.left.left = new_Node(4)
    root.right.left = new_Node(5)
    root.right.right = new_Node(6)
    root.right.left.right = new_Node(7)
    root.right.right.right = new_Node(8)
    root.right.left.right.left = new_Node(9)
     
    # Calculating height of tree
    levels = height(root)
     
    # Printing the deepest node
    deepestNode(root, levels)
 
# This code is contributed by PranchalK
 
 

C#




// C# program to find value of the
// deepest node in a given binary tree
using System;
     
class GFG
{
     
// A tree node with constructor
public class Node
{
    public int data;
    public Node left, right;
     
    // constructor
    public Node(int key)
    {
        data = key;
        left = null;
        right = null;
    }
};
 
// Utility function to find height
// of a tree, rooted at 'root'.
static int height(Node root)
{
    if(root == null) return 0;
         
    int leftHt = height(root.left);
    int rightHt = height(root.right);
         
    return Math.Max(leftHt, rightHt) + 1;
}
 
// levels : current Level
// Utility function to print all
// nodes at a given level.
static void deepestNode(Node root,
                        int levels)
{
    if(root == null) return;
     
    if(levels == 1)
    Console.Write(root.data + " ");
     
    else if(levels > 1)
    {
        deepestNode(root.left, levels - 1);
        deepestNode(root.right, levels - 1);
    }
}
 
// Driver Code
public static void Main(String []args)
{
    Node root = new Node(1);
    root.left = new Node(2);
    root.right = new Node(3);
    root.left.left = new Node(4);
    root.right.left = new Node(5);
    root.right.right = new Node(6);
    root.right.left.right = new Node(7);
    root.right.right.right = new Node(8);
    root.right.left.right.left = new Node(9);
     
    // Calculating height of tree
    int levels = height(root);
     
    // Printing the deepest node
    deepestNode(root, levels);
}
}
 
/* This code contributed by PrinciRaj1992 */
 
 

Javascript




<script>
// A Javascript program to find value of the
// deepest node in a given binary tree
 
// A tree node with constructor
class Node
{
    // constructor
    constructor(key)
    {
        this.data = key;
        this.left = null;
        this.right = null;
    }
}
 
// Utility function to find height
// of a tree, rooted at 'root
function height(root)
{
    if(root == null) return 0;
          
    let leftHt = height(root.left);
    let rightHt = height(root.right);
          
    return Math.max(leftHt, rightHt) + 1;
}
 
// levels : current Level
// Utility function to print all
// nodes at a given level.
function deepestNode(root,levels)
{
    if(root == null) return;
      
    if(levels == 1)
    document.write(root.data + " ");
      
    else if(levels > 1)
    {
        deepestNode(root.left, levels - 1);
        deepestNode(root.right, levels - 1);
    }
}
 
// Driver Codede
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.right = new Node(6);
root.right.left.right = new Node(7);
root.right.right.right = new Node(8);
root.right.left.right.left = new Node(9);
 
// Calculating height of tree
let levels = height(root);
 
// Printing the deepest node
deepestNode(root, levels);
 
 
 
// This code is contributed by avanitrachhadiya2155
</script>
 
 
Output
9

Time Complexity: O(n)

Space Complexity : O(n)

Method 3: The last node processed from the queue in level order is the deepest node in the binary tree.

Implementation:

C++




// A C++ program to find value of the
// deepest node in a given binary tree
#include <bits/stdc++.h>
using namespace std;
 
// A tree node with constructor
class Node
{
public:
    int data;
    Node *left, *right;
      
    // constructor  
    Node(int key)
    {
        data = key;
        left = NULL;
        right = NULL;
    }
};
 
// Function to return the deepest node
Node* deepestNode(Node* root)
{
    Node* tmp = NULL;
    if (root == NULL)
        return NULL;
 
    // Creating a Queue
    queue<Node*> q;
    q.push(root);
 
    // Iterates until queue become empty
    while (q.size() > 0)
    {
        tmp = q.front();
        q.pop();
        if (tmp->left != NULL)
            q.push(tmp->left);
        if (tmp->right != NULL)
            q.push(tmp->right);
    }
    return tmp;
}
     
int main()
{
    Node* root = new Node(1);
    root->left = new Node(2);
    root->right = new Node(3);
    root->left->left = new Node(4);
    root->right->left = new Node(5);
    root->right->right = new Node(6);
    root->right->left->right = new Node(7);
    root->right->right->right = new Node(8);
    root->right->left->right->left = new Node(9);
 
    Node* deepNode = deepestNode(root);
    cout << (deepNode->data);
 
    return 0;
}
 
 

Java




import java.util.*;
 
// A Java program to find value of the
// deepest node in a given binary tree
 
// A tree node with constructor
public class Node
{
    int data;
    Node left, right;
 
    // constructor
    Node(int key)
    {
        data = key;
        left = null;
        right = null;
    }
};
 
class Gfg
{
   
    // Function to return the deepest node
    public static Node deepestNode(Node root)
    {
        Node tmp = null;
        if (root == null)
            return null;
 
        // Creating a Queue
        Queue<Node> q = new LinkedList<Node>();
        q.offer(root);
 
        // Iterates until queue become empty
        while (!q.isEmpty())
        {
            tmp = q.poll();
            if (tmp.left != null)
                q.offer(tmp.left);
            if (tmp.right != null)
                q.offer(tmp.right);
        }
        return tmp;
    }
 
    public static void main(String[] args)
    {
 
        Node root = new Node(1);
        root.left = new Node(2);
        root.right = new Node(3);
        root.left.left = new Node(4);
        root.right.left = new Node(5);
        root.right.right = new Node(6);
        root.right.left.right = new Node(7);
        root.right.right.right = new Node(8);
        root.right.left.right.left = new Node(9);
 
        Node deepNode = deepestNode(root);
        System.out.println(deepNode.data);
    }
}
 
// Code is contributed by mahi_07
 
 

Python3




# A Python3 program to find value of the
# deepest node in a given binary tree by method 3
from collections import deque
 
class new_Node:
    def __init__(self, key):
        self.data = key
        self.left = self.right = None
 
def deepestNode(root):
    if root == None:
        return 0
    q = deque()
    q.append(root)
    node = None
    while len(q) != 0:
        node = q.popleft()
        if node.left is not None:
            q.append(node.left)
        if node.right is not None:
            q.append(node.right)
    return node.data
  
# Driver Code
if __name__ == '__main__':
  
    root = new_Node(1)
    root.left = new_Node(2)
    root.right = new_Node(3)
    root.left.left = new_Node(4)
    root.right.left = new_Node(5)
    root.right.right = new_Node(6)
    root.right.left.right = new_Node(7)
    root.right.right.right = new_Node(8)
    root.right.left.right.left = new_Node(9)
      
    # Calculating height of tree
    levels = deepestNode(root)
      
    # Printing the deepest node
    print(levels)
     
# This code is contributed by Aprajita Chhawi
 
 

C#




// A C# program to find value of the
// deepest node in a given binary tree
using System;
using System.Collections.Generic;
 
// A tree node with constructor
public class Node
{
  public
    int data;
  public
    Node left, right;
 
  // constructor
  public
    Node(int key)
  {
    data = key;
    left = null;
    right = null;
  }
};
 
class Gfg
{
 
  // Function to return the deepest node
  public static Node deepestNode(Node root)
  {
    Node tmp = null;
    if (root == null)
      return null;
 
    // Creating a Queue
    Queue<Node> q = new Queue<Node>();
    q.Enqueue(root);
 
    // Iterates until queue become empty
    while (q.Count != 0)
    {
      tmp = q.Peek();
      q.Dequeue();
      if (tmp.left != null)
        q.Enqueue(tmp.left);
      if (tmp.right != null)
        q.Enqueue(tmp.right);
    }
    return tmp;
  }
 
  // Driver code
  public static void Main(String[] args)
  {
 
    Node root = new Node(1);
    root.left = new Node(2);
    root.right = new Node(3);
    root.left.left = new Node(4);
    root.right.left = new Node(5);
    root.right.right = new Node(6);
    root.right.left.right = new Node(7);
    root.right.right.right = new Node(8);
    root.right.left.right.left = new Node(9);
 
    Node deepNode = deepestNode(root);
    Console.WriteLine(deepNode.data);
  }
}
 
// This code is contributed by gauravrajput1
 
 

Javascript




<script>
// A Javascript program to find value of the
// deepest node in a given binary tree
  
// A tree node with constructor
class Node
{
    constructor(key)
    {
        this.data = key;
        this.left = null;
        this.right = null;
    }
}
 
// Function to return the deepest node
function deepestNode(root)
{
    let  tmp = null;
        if (root == null)
            return null;
  
        // Creating a Queue
        let q = [];
        q.push(root);
  
        // Iterates until queue become empty
        while (q.length!=0)
        {
            tmp = q.shift();
            if (tmp.left != null)
                q.push(tmp.left);
            if (tmp.right != null)
                q.push(tmp.right);
        }
        return tmp;
}
 
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.right.left = new Node(5);
root.right.right = new Node(6);
root.right.left.right = new Node(7);
root.right.right.right = new Node(8);
root.right.left.right.left = new Node(9);
 
let deepNode = deepestNode(root);
document.write(deepNode.data);
 
// This code is contributed by unknown2108
</script>
 
 
Output
9

Time Complexity: O(n) 

Auxiliary Space: O(n)



Next Article
Find Minimum Depth of a Binary Tree

R

Rakesh Kumar
Improve
Article Tags :
  • DSA
  • Tree
  • Binary Tree
Practice Tags :
  • Tree

Similar Reads

  • Find the closest leaf in a Binary Tree
    Given a Binary Tree and a key 'k', find distance of the closest leaf from 'k'. Examples: A / \ B C / \ E F / \ G H / \ / I J K Closest leaf to 'H' is 'K', so distance is 1 for 'H' Closest leaf to 'C' is 'B', so distance is 2 for 'C' Closest leaf to 'E' is either 'I' or 'J', so distance is 2 for 'E'
    14 min read
  • Deepest left leaf node in a binary tree
    Given a Binary Tree, find the deepest leaf node that is left child of its parent. For example, consider the following tree. The deepest left leaf node is the node with value 9. 1 / \ 2 3 / / \ 4 5 6 \ \ 7 8 / \ 9 10 The idea is to recursively traverse the given binary tree and while traversing, main
    13 min read
  • Find Minimum Depth of a Binary Tree
    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. For example, minimum depth of below Binary Tree is 2. Note that the path must end on a leaf node. For example, the minimum depth of below Bi
    15 min read
  • Depth of the deepest odd level node in Binary Tree
    Given a Binary tree, find out depth of the deepest odd level leaf node. Take root level as depth 1. Examples:  Input : Output : 5Input : 10 / \ 28 13 / \ 14 15 / \ 23 24Output : 3We can traverse the tree starting from the root level and keep curr_level of the node. Increment the curr_level each time
    14 min read
  • Closest leaf to a given node in Binary Tree
    Given a Binary Tree and a node x in it, find distance of the closest leaf to x in Binary Tree. If given node itself is a leaf, then distance is 0.Examples: Input: Root of below tree And x = pointer to node 13 10 / \ 12 13 / 14 Output 1 Distance 1. Closest leaf is 14. Input: Root of below tree And x
    12 min read
  • Height and Depth of a node in a Binary Tree
    Given a Binary Tree consisting of n nodes and a integer k, the task is to find the depth and height of the node with value k in the Binary Tree. Note: The depth of a node is the number of edges present in path from the root node of a tree to that node. The height of a node is the maximum number of e
    15 min read
  • Replace node with depth in a binary tree
    Given a binary tree, replace each node with its depth value. For example, consider the following tree. Root is at depth 0, change its value to 0 and next level nodes are at depth 1 and so on. 3 0 / \ / \ 2 5 == >; 1 1 / \ / \ 1 4 2 2 The idea is to traverse tree starting from root. While traversi
    11 min read
  • Find Mode in Binary Search tree
    Given a Binary Search Tree, find the mode of the tree. Note: Mode is the value of the node which has the highest frequency in the binary search tree. Examples: Input: 100 / \ 50 160 / \ / \ 50 60 140 170 Output: The mode of BST is 50Explanation: 50 is repeated 2 times, and all other nodes occur only
    10 min read
  • Find the closest element in Binary Search Tree
    Given a binary search tree and a target node K. The task is to find the node with a minimum absolute difference with the given target value K. Examples: Input : k = 4Output: 4 Input : k = 18Output: 17 Input : k = 12Output: 9 Input: k=2Output: 3 [Naive Approach] By using Inorder Traversal:Store Inord
    5 min read
  • Find next smaller element in Binary Search Tree
    Given a binary search tree and a target value, the task is to find the next smaller element of the target value in the binary search tree. Examples : Input: 8 / \ 3 10 / \ \ 1 6 14 / \ / 4 7 13 Target: 7Output: 6Explanation: The next smaller element of 7 is 6 Input: 6 / \ 4 8 / \ / \ 2 5 7 10 Target
    15+ min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences