Coefficient of Range in a Binary Tree

Given a Binary Tree, the task is to find the Coefficient of Range in it.

Range is defined as the difference between the maximum and minimum value in a set of data and Coefficient of Range is the relative measure of the dispersion of the range. Suppose the maximum value in a data set is maxVal and minimum value is minVal then the coefficient of range can be defined as:

Coefficient of range = (maxVal – minVal)/(maxVal + minVal)

Consider the below Binary Tree:

For example, maximum in the above Binary Tree is 9 and minimum is 1 so coeffcient of range is ((9 – 1)/ ( 9 + 1)) = 0.8.

Approach: In Binary Search Tree, we can find maximum by traversing right pointers until we reach rightmost node. But in Binary Tree, we must visit every node to figure out maximum. So the idea is to traverse the given tree and for every node return maximum of 3 values.

  1. Node’s data.
  2. Maximum in node’s left subtree.
  3. Maximum in node’s right subtree.

Similarly, find the minimum value in the Binary Tree and calculate the coefficient of range.

Below is the implementation of the above approach:





// CPP program to find Coefficient of
// Range in a Bianry Tree
#include <bits/stdc++.h>
using namespace std;
// A tree node
struct Node {
    float data;
    struct Node *left, *right;
// A utility function to create a new node
struct Node* newNode(float data)
    struct Node* node = (struct Node*)
        malloc(sizeof(struct Node));
    node->data = data;
    node->left = node->right = NULL;
    return (node);
// Returns maximum value in a given Binary Tree
float findMax(struct Node* root)
    // Base case
    if (root == NULL)
        return INT_MIN;
    // Return maximum of 3 values:
    // 1) Root's data 2) Max in Left Subtree
    // 3) Max in right subtree
    float res = root->data;
    float lres = findMax(root->left);
    float rres = findMax(root->right);
    if (lres > res)
        res = lres;
    if (rres > res)
        res = rres;
    return res;
// Returns minimum value in a given Binary Tree
float findMin(struct Node* root)
    // Base case
    if (root == NULL)
        return INT_MAX;
    // Return minimum of 3 values:
    // 1) Root's data 2) Min in Left Subtree
    // 3) Min in right subtree
    float res = root->data;
    float lres = findMin(root->left);
    float rres = findMin(root->right);
    if (lres < res)
        res = lres;
    if (rres < res)
        res = rres;
    return res;
// Function to find the value of the Coefficient
// of range in the Binary Tree
float coefRange(Node* root)
    float max = findMax(root);
    float min = findMin(root);
    return (max - min) / (max + min);
// Driver Code
int main(void)
    // Contruct the Binary Tree
    struct Node* root = newNode(2);
    root->left = newNode(7);
    root->right = newNode(5);
    root->left->right = newNode(6);
    root->left->right->left = newNode(1);
    root->left->right->right = newNode(11);
    root->right->right = newNode(9);
    root->right->right->left = newNode(4);
    cout << "Coefficient of Range is " << coefRange(root);
    return 0;



Coefficient of Range is 0.833333

Time complexity : O(n) where n is the number of nodes.

My Personal Notes arrow_drop_up