# Find maximum in binary tree in C

## Maximum in Binary Tree in C

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.

## Algorithm :

• If root is NULL, return minimum integer.
• Otherwise, recursive call the function for left and right subtree.
• Return the maximum value that is max of the right subtree, left subtree and the data present in root.

## Code in C based on above ALgorithm

`#include <limits.h>#include <stdio.h>#include <stdlib.h> struct Node {    int data;    struct Node *left, *right;}; struct Node* newNode(int data){    struct Node* node = (struct Node*)malloc(sizeof(struct Node));    node->data = data;    node->left = node->right = NULL;    return (node);}int findMax(struct Node* root){    if (root == NULL)        return INT_MIN;     int res = root->data;    int lres = findMax(root->left);    int rres = findMax(root->right);    if (lres > res)        res = lres;    if (rres > res)        res = rres;    return res;} int main( ){    struct Node* NewRoot = NULL;    struct Node* root = newNode(12);    root->left = newNode(17);    root->right = newNode(50);    root->left->right = newNode(67);    root->left->right->left = newNode(10);    root->left->right->right = newNode(11);    root->right->right = newNode(98);    root->right->right->left = newNode(14);     printf("Maximum element is %d \n", findMax(root));     return 0;}`
`Output :Maximum element is 98`