Find Max and Min Element in Binary Tree

Last Updated : 20 Aug, 2026

Given the root of a Binary Tree, find the maximum and minimum element present in the tree.

Examples:

Input: root[] = [2, 7, 5, N, 6, N, 9, 1, 11, 4]

3

Output: 11 1
Explanation: The maximum and minimum element in this binary tree is 11 and 1 respectively.

Input: root[] = [6, 5, 8, 2]

4


Output: 8 2
Explanation: The maximum and minimum element in this binary tree is 8 and 2 respectively.

Try It Yourself
redirect icon

Traversal with Current Maximum/Minimum - O(n) Time and O(h) Space

The idea is to traverse the binary tree recursively and keep updating the current maximum and minimum values whenever a node is visited.

Working of Approach:

  • Initialize maximum as INT_MIN and minimum as INT_MAX.
  • Traverse the binary tree using DFS.
  • Update the maximum and minimum using the current node's value.
  • Continue the traversal for the left and right subtrees.
  • After the traversal, return the final maximum and minimum values.
C++
#include <bits/stdc++.h>
using namespace std;

// Structure of a Binary Tree Node
class Node
{
  public:
    int data;
    Node *left;
    Node *right;

    Node(int x)
    {
        data = x;
        left = right = nullptr;
    }
};

// DFS traversal to update maximum and minimum values
void dfs(Node *root, int &mx, int &mn)
{

    // Base Case
    if (root == nullptr)
        return;

    // Update maximum and minimum values
    mx = max(mx, root->data);
    mn = min(mn, root->data);

    // Recur for left and right subtrees
    dfs(root->left, mx, mn);
    dfs(root->right, mx, mn);
}

// Function to find the maximum element
int findMax(Node *root)
{
    int mx = INT_MIN;
    int mn = INT_MAX;

    dfs(root, mx, mn);

    return mx;
}

// Function to find the minimum element
int findMin(Node *root)
{
    int mx = INT_MIN;
    int mn = INT_MAX;

    dfs(root, mx, mn);

    return mn;
}

int main()
{

    // Construct the binary tree
    //        6
    //      /   \
    //     5     8
    //    /
    //   2

    Node *root = new Node(6);
    root->left = new Node(5);
    root->right = new Node(8);
    root->left->left = new Node(2);

    cout << findMax(root) << " ";
    cout << findMin(root);

    return 0;
}
Java
import java.util.*;

// Structure of a Binary Tree Node
class Node {
    public int data;
    Node left;
    Node right;

    Node(int x)
    {
        data = x;
        left = right = null;
    }
}

public class GFG {

    // DFS traversal to update maximum and minimum values
    static void dfs(Node root, int[] mx, int[] mn)
    {
        // Base Case
        if (root == null)
            return;

        // Update maximum and minimum values
        mx[0] = Math.max(mx[0], root.data);
        mn[0] = Math.min(mn[0], root.data);

        // Recur for left and right subtrees
        dfs(root.left, mx, mn);
        dfs(root.right, mx, mn);
    }

    // Function to find the maximum element
    static int findMax(Node root)
    {
        int[] mx = { Integer.MIN_VALUE };
        int[] mn = { Integer.MAX_VALUE };

        dfs(root, mx, mn);

        return mx[0];
    }

    // Function to find the minimum element
    static int findMin(Node root)
    {
        int[] mx = { Integer.MIN_VALUE };
        int[] mn = { Integer.MAX_VALUE };

        dfs(root, mx, mn);

        return mn[0];
    }

    public static void main(String[] args)
    {
        // Construct the binary tree
        //        6
        //      /   \
        //     5     8
        //    /
        //   2

        Node root = new Node(6);
        root.left = new Node(5);
        root.right = new Node(8);
        root.left.left = new Node(2);

        System.out.print(findMax(root) + " ");
        System.out.print(findMin(root));
    }
}
Python
# Structure of a Binary Tree Node
class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None

# DFS traversal to update maximum and minimum values


def dfs(root, mx, mn):
    # Base Case
    if root is None:
        return

    # Update maximum and minimum values
    mx[0] = max(mx[0], root.data)
    mn[0] = min(mn[0], root.data)

    # Recur for left and right subtrees
    dfs(root.left, mx, mn)
    dfs(root.right, mx, mn)

# Function to find the maximum element


def findMax(root):
    mx = [float('-inf')]
    mn = [float('inf')]

    dfs(root, mx, mn)

    return mx[0]

# Function to find the minimum element


def findMin(root):
    mx = [float('-inf')]
    mn = [float('inf')]

    dfs(root, mx, mn)

    return mn[0]


if __name__ == '__main__':
    # Construct the binary tree
    #        6
    #      /   \
    #     5     8
    #    /
    #   2

    root = Node(6)
    root.left = Node(5)
    root.right = Node(8)
    root.left.left = Node(2)

    print(findMax(root), end=' ')
    print(findMin(root))
C#
using System;

// Structure of a Binary Tree Node
public class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int x)
    {
        data = x;
        left = right = null;
    }
}

public class GFG {
    // DFS traversal to update maximum and minimum values
    static void DFS(Node root, ref int mx, ref int mn)
    {
        // Base Case
        if (root == null)
            return;

        // Update maximum and minimum values
        mx = Math.Max(mx, root.data);
        mn = Math.Min(mn, root.data);

        // Recur for left and right subtrees
        DFS(root.left, ref mx, ref mn);
        DFS(root.right, ref mx, ref mn);
    }

    // Function to find the maximum element
    static int findMax(Node root)
    {
        int mx = int.MinValue;
        int mn = int.MaxValue;

        DFS(root, ref mx, ref mn);

        return mx;
    }

    // Function to find the minimum element
    static int findMin(Node root)
    {
        int mx = int.MinValue;
        int mn = int.MaxValue;

        DFS(root, ref mx, ref mn);

        return mn;
    }

    public static void Main()
    {
        // Construct the binary tree
        //        6
        //      /   \
        //     5     8
        //    /
        //   2

        Node root = new Node(6);
        root.left = new Node(5);
        root.right = new Node(8);
        root.left.left = new Node(2);

        Console.Write(findMax(root) + " ");
        Console.Write(findMin(root));
    }
}
JavaScript
// Structure of a Binary Tree Node
class Node {
    constructor(x)
    {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// DFS traversal to update maximum and minimum values
function dfs(root, mx, mn)
{
    // Base Case
    if (root === null)
        return;

    // Update maximum and minimum values
    mx[0] = Math.max(mx[0], root.data);
    mn[0] = Math.min(mn[0], root.data);

    // Recur for left and right subtrees
    dfs(root.left, mx, mn);
    dfs(root.right, mx, mn);
}

// Function to find the maximum element
function findMax(root)
{
    let mx = [ Number.NEGATIVE_INFINITY ];
    let mn = [ Number.POSITIVE_INFINITY ];

    dfs(root, mx, mn);

    return mx[0];
}

// Function to find the minimum element
function findMin(root)
{
    let mx = [ Number.NEGATIVE_INFINITY ];
    let mn = [ Number.POSITIVE_INFINITY ];

    dfs(root, mx, mn);

    return mn[0];
}

// Driver Code
// Construct the binary tree
//        6
//      /   \
//     5     8
//    /
//   2

let root = new Node(6);
root.left = new Node(5);
root.right = new Node(8);
root.left.left = new Node(2);

console.log(findMax(root) + " ");
console.log(findMin(root));

Output
8 2

Separate Traversals for Max and Min - O(n) Time and O(h) Space

The idea is to recursively find the maximum (or minimum) value in the left and right subtrees and combine them with the current node's value to get the answer for the current subtree.

Working of Approach:

  • If the current node is NULL, return INT_MIN for maximum and INT_MAX for minimum.
  • Recursively find the answer for the left subtree.
  • Recursively find the answer for the right subtree.
  • Return the maximum (or minimum) among the current node and both subtree results.
  • The root call returns the maximum and minimum values of the entire tree.

Let us understand with an example:
Input: root[] = [6, 5, 8, 2]

4
  • Start from the root node (6). The recursion explores both the left and right subtrees before computing the maximum and minimum values.
  • In the left subtree, nodes 5 and 2 are visited. The maximum returned is 5 and the minimum returned is 2.
  • In the right subtree, node 8 is visited. It returns 8 as both the maximum and minimum for that subtree.
  • At the root, findMax() computes max(6, 5, 8) = 8, while findMin() computes min(6, 2, 8) = 2.
  • Hence, the maximum element in the tree is 8 and the minimum element is 2.
C++
#include <bits/stdc++.h>
using namespace std;

// Structure of a Binary Tree Node
class Node
{
  public:
    int data;
    Node *left;
    Node *right;

    Node(int x)
    {
        data = x;
        left = right = nullptr;
    }
};

// Function to find the maximum value in a binary tree.
int findMax(Node *root)
{
    // If root is null, return INT_MIN as there is no value to compare.
    if (root == nullptr)
    {
        return INT_MIN;
    }

    // Return the maximum value among root's data and the maximum values
    // in its left and right subtrees.
    return max({root->data, findMax(root->left), findMax(root->right)});
}

// Function to find the minimum value in a binary tree.
int findMin(Node *root)
{
    // If root is null, return INT_MAX as there is no value to compare.
    if (root == nullptr)
    {
        return INT_MAX;
    }

    // Return the minimum value among root's data and the minimum values
    // in its left and right subtrees.
    return min({root->data, findMin(root->left), findMin(root->right)});
}

int main()
{

    // Construct the binary tree
    //        6
    //      /   \
    //     5     8
    //    /
    //   2

    Node *root = new Node(6);
    root->left = new Node(5);
    root->right = new Node(8);
    root->left->left = new Node(2);

    cout << findMax(root) << " ";
    cout << findMin(root);

    return 0;
}
Java
import java.util.Arrays;

// Structure of a Binary Tree Node
class Node {
  public int data;
  public Node left;
  public Node right;

  public Node(int x) {
    data = x;
    left = right = null;
  }
}

public class GFG {
  // Function to find the maximum value in a binary tree.
  public static int findMax(Node root) {
    // If root is null, return Integer.MIN_VALUE as there is no value to compare.
    if (root == null) {
      return Integer.MIN_VALUE;
    }

    // Return the maximum value among root's data and the maximum values
    // in its left and right subtrees.
    return Arrays.stream(new int[] {root.data, findMax(root.left), findMax(root.right)}).max().getAsInt();
  }

  // Function to find the minimum value in a binary tree.
  public static int findMin(Node root) {
    // If root is null, return Integer.MAX_VALUE as there is no value to compare.
    if (root == null) {
      return Integer.MAX_VALUE;
    }

    // Return the minimum value among root's data and the minimum values
    // in its left and right subtrees.
    return Arrays.stream(new int[] {root.data, findMin(root.left), findMin(root.right)}).min().getAsInt();
  }

  public static void main(String[] args) {

    // Construct the binary tree
    //        6
    //      /   \
    //     5      8
    //    /
    //   2

    Node root = new Node(6);
    root.left = new Node(5);
    root.right = new Node(8);
    root.left.left = new Node(2);

    System.out.print(findMax(root) + " ");
    System.out.print(findMin(root));
  }
}
Python
"""
Structure of a Binary Tree Node
"""


class Node:
    def __init__(self, x):
        self.data = x
        self.left = None
        self.right = None

# Function to find the maximum value in a binary tree.


def findMax(root):
    # If root is null, return float('-inf') as there is no value to compare.
    if root is None:
        return float('-inf')

    # Return the maximum value among root's data and the maximum values
    # in its left and right subtrees.
    return max(root.data, findMax(root.left), findMax(root.right))

# Function to find the minimum value in a binary tree.


def findMin(root):
    # If root is null, return float('inf') as there is no value to compare.
    if root is None:
        return float('inf')

    # Return the minimum value among root's data and the minimum values
    # in its left and right subtrees.
    return min(root.data, findMin(root.left), findMin(root.right))


if __name__ == '__main__':

    # Construct the binary tree
    #        6
    #      /   \\
    #     5     8
    #    /
    #   2

    root = Node(6)
    root.left = Node(5)
    root.right = Node(8)
    root.left.left = Node(2)

    print(findMax(root), end=' ')
    print(findMin(root))
C#
using System;

// Structure of a Binary Tree Node
public class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int x)
    {
        data = x;
        left = right = null;
    }
}

public class GFG {
    // Function to find the maximum value in a binary tree.
    public static int findMax(Node root)
    {
        // If root is null, return int.MinValue as there is
        // no value to compare.
        if (root == null) {
            return int.MinValue;
        }

        // Return the maximum value among root's data and
        // the maximum values in its left and right
        // subtrees.
        return Math.Max(root.data,
                        Math.Max(findMax(root.left),
                                 findMax(root.right)));
    }

    // Function to find the minimum value in a binary tree.
    public static int findMin(Node root)
    {
        // If root is null, return int.MaxValue as there is
        // no value to compare.
        if (root == null) {
            return int.MaxValue;
        }

        // Return the minimum value among root's data and
        // the minimum values in its left and right
        // subtrees.
        return Math.Min(root.data,
                        Math.Min(findMin(root.left),
                                 findMin(root.right)));
    }

    public static void Main()
    {
        // Construct the binary tree
        //        6
        //      /   \\
        //     5     8
        //    /
        //   2

        Node root = new Node(6);
        root.left = new Node(5);
        root.right = new Node(8);
        root.left.left = new Node(2);

        Console.Write(findMax(root) + " ");
        Console.Write(findMin(root));
    }
}
JavaScript
// Structure of a Binary Tree Node
class Node {
    constructor(x)
    {
        this.data = x;
        this.left = null;
        this.right = null;
    }
}

// Function to find the maximum value in a binary tree.
function findMax(root)
{
    // If root is null, return Number.NEGATIVE_INFINITY as
    // there is no value to compare.
    if (root === null) {
        return Number.NEGATIVE_INFINITY;
    }

    // Return the maximum value among root's data and the
    // maximum values in its left and right subtrees.
    return Math.max(root.data, findMax(root.left),
                    findMax(root.right));
}

// Function to find the minimum value in a binary tree.
function findMin(root)
{
    // If root is null, return Number.POSITIVE_INFINITY as
    // there is no value to compare.
    if (root === null) {
        return Number.POSITIVE_INFINITY;
    }

    // Return the minimum value among root's data and the
    // minimum values in its left and right subtrees.
    return Math.min(root.data, findMin(root.left),
                    findMin(root.right));
}

// Driver Code
// Construct the binary tree
//        6
//      /   \\
//     5     8
//    /
//   2

const root = new Node(6);
root.left = new Node(5);
root.right = new Node(8);
root.left.left = new Node(2);

console.log(findMax(root) + " ");
console.log(findMin(root));

Output
8 2
Comment