Find Maximum Element in Binary Tree in C++

Maximum In Binary Tree

Given a binary tree, we need to find the maximum element in the binary tree. As we need to visit each node present in the tree, we can use any tree traversal or recursion. In this page, Recursion is used.

Finding maximum element in a binary tree


  1. If root is NULL, return minimum interger.
  2. Otherwise, recursive call the function for left and right subtree.
  3. Return the maximum value that is max of the right subtree, left subtree and the data present in root.
To find maximum number in a binary tree C++


using namespace std
class Tree {
        int data;
        Tree* left = NULL,*right = NULL;
        // Constructor initialised
        Tree(int x) {
            data = x;
            left = NULL;
            right = NULL;
int maximum_tree(Tree *root) {
    if (root == NULLreturn INT_MIN; // If null return minimum number
    // Else return the max of number in left subtree, right subtree and the data
    return max(root -> data,max(maximum_tree(root->left),maximum_tree(root -> right)));
int main() {
    Tree *root = new Tree(10);
    root -> left = new Tree(22);
    root -> right = new Tree(10);
    root -> left -> left = new Tree(11);
    root -> left -> right = new Tree(10);
    cout << maximum_tree(root);
    return 0;




Time Complexity To Find Maximum In Binary Tree

Time Complexity


Space Complexity