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.
- Node’s data.
- Maximum in node’s left subtree.
- 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:
Coefficient of Range is 0.833333
Time complexity : O(n) where n is the number of nodes.
- Binary Indexed Tree : Range Update and Range Queries
- Range and Coefficient of range of Array
- Binary Indexed Tree : Range Updates and Point Queries
- Replace the maximum element in the array by coefficient of range
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Minimum swap required to convert binary tree to binary search tree
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Convert a Binary Tree to Threaded binary tree | Set 2 (Efficient)
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue)
- Check whether a binary tree is a full binary tree or not
- Binary Tree to Binary Search Tree Conversion using STL set
- Check whether a given binary tree is skewed binary tree or not?
- Binary Tree to Binary Search Tree Conversion
- Check if a binary tree is subtree of another binary tree | Set 1
- Check if a binary tree is subtree of another binary tree | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : VishalBachchas