In a Red-Black Tree, the maximum height of a node is at most twice the minimum height (The four Red-Black tree properties make sure this is always followed). Given a Binary Search Tree, we need to check for following property.
For every node, length of the longest leaf to node path has not more than twice the nodes on shortest path from node to leaf.
12 40
\ / \
14 10 100
\ / \
16 60 150
Cannot be a Red-Black Tree It can be Red-Black Tree
with any color assignment
Max height of 12 is 1
Min height of 12 is 3
10
/ \
5 100
/ \
50 150
/
40
It can also be Red-Black Tree
Expected time complexity is O(n). The tree should be traversed at-most once in the solution.
We strongly recommend to minimize the browser and try this yourself first.
For every node, we need to get the maximum and minimum heights and compare them. The idea is to traverse the tree and for every node check if it’s balanced. We need to write a recursive function that returns three things, a boolean value to indicate the tree is balanced or not, minimum height and maximum height. To return multiple values, we can either use a structure or pass variables by reference. We have passed maxh and minh by reference so that the values can be used in parent calls.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int key;
Node *left, *right;
};
Node* newNode( int key)
{
Node* node = new Node;
node->key = key;
node->left = node->right = NULL;
return (node);
}
bool isBalancedUtil(Node *root, int &maxh, int &minh)
{
if (root == NULL)
{
maxh = minh = 0;
return true ;
}
int lmxh, lmnh;
int rmxh, rmnh;
if (isBalancedUtil(root->left, lmxh, lmnh) == false )
return false ;
if (isBalancedUtil(root->right, rmxh, rmnh) == false )
return false ;
maxh = max(lmxh, rmxh) + 1;
minh = min(lmnh, rmnh) + 1;
if (maxh <= 2*minh)
return true ;
return false ;
}
bool isBalanced(Node *root)
{
int maxh, minh;
return isBalancedUtil(root, maxh, minh);
}
int main()
{
Node * root = newNode(10);
root->left = newNode(5);
root->right = newNode(100);
root->right->left = newNode(50);
root->right->right = newNode(150);
root->right->left->left = newNode(40);
isBalanced(root)? cout << "Balanced" : cout << "Not Balanced" ;
return 0;
}
|
Java
class GFG
{
static class Node
{
int key;
Node left, right;
Node( int key)
{
left = null ;
right = null ;
this .key = key;
}
}
static class INT
{
static int d;
INT()
{
d = 0 ;
}
}
static boolean isBalancedUtil(Node root,
INT maxh, INT minh)
{
if (root == null )
{
maxh.d = minh.d = 0 ;
return true ;
}
INT lmxh= new INT(), lmnh= new INT();
INT rmxh= new INT(), rmnh= new INT();
if (isBalancedUtil(root.left, lmxh, lmnh) == false )
return false ;
if (isBalancedUtil(root.right, rmxh, rmnh) == false )
return false ;
maxh.d = Math.max(lmxh.d, rmxh.d) + 1 ;
minh.d = Math.min(lmnh.d, rmnh.d) + 1 ;
if (maxh.d <= 2 *minh.d)
return true ;
return false ;
}
static boolean isBalanced(Node root)
{
INT maxh= new INT(), minh= new INT();
return isBalancedUtil(root, maxh, minh);
}
public static void main(String args[])
{
Node root = new Node( 10 );
root.left = new Node( 5 );
root.right = new Node( 100 );
root.right.left = new Node( 50 );
root.right.right = new Node( 150 );
root.right.left.left = new Node( 40 );
System.out.println(isBalanced(root) ?
"Balanced" : "Not Balanced" );
}
}
|
Python3
class newNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def isBalancedUtil(root, maxh, minh) :
if (root = = None ) :
maxh = minh = 0
return True
lmxh = 0
lmnh = 0
rmxh, rmnh = 0 , 0
if (isBalancedUtil(root.left, lmxh, lmnh) = = False ) :
return False
if (isBalancedUtil(root.right, rmxh, rmnh) = = False ) :
return False
maxh = max (lmxh, rmxh) + 1
minh = min (lmnh, rmnh) + 1
if (maxh < = 2 * minh) :
return True
return False
def isBalanced(root) :
maxh, minh = 0 , 0
return isBalancedUtil(root, maxh, minh)
if __name__ = = '__main__' :
root = newNode( 10 )
root.left = newNode( 5 )
root.right = newNode( 100 )
root.right.left = newNode( 50 )
root.right.right = newNode( 150 )
root.right.left.left = newNode( 40 )
if (isBalanced(root)):
print ( "Balanced" )
else :
print ( "Not Balanced" )
|
C#
using System;
class GFG
{
public class Node
{
public int key;
public Node left, right;
public Node( int key)
{
left = null ;
right = null ;
this .key = key;
}
}
public class INT
{
public int d;
public INT()
{
d = 0;
}
}
static bool isBalancedUtil(Node root,
INT maxh, INT minh)
{
if (root == null )
{
maxh.d = minh.d = 0;
return true ;
}
INT lmxh = new INT(), lmnh = new INT();
INT rmxh = new INT(), rmnh = new INT();
if (isBalancedUtil(root.left, lmxh, lmnh) == false )
return false ;
if (isBalancedUtil(root.right, rmxh, rmnh) == false )
return false ;
maxh.d = Math.Max(lmxh.d, rmxh.d) + 1;
minh.d = Math.Min(lmnh.d, rmnh.d) + 1;
if (maxh.d <= 2 * minh.d)
return true ;
return false ;
}
static bool isBalanced(Node root)
{
INT maxh = new INT(), minh = new INT();
return isBalancedUtil(root, maxh, minh);
}
public static void Main(String []args)
{
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(100);
root.right.left = new Node(50);
root.right.right = new Node(150);
root.right.left.left = new Node(40);
Console.WriteLine(isBalanced(root) ?
"Balanced" : "Not Balanced" );
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .key = val;
this .left = null ;
this .right = null ;
}
}
class INT {
constructor() {
this .d = 0;
}
}
function isBalancedUtil(root, maxh, minh) {
if (root == null ) {
maxh.d = minh.d = 0;
return true ;
}
var lmxh = new INT(), lmnh = new INT();
var rmxh = new INT(), rmnh = new INT();
if (isBalancedUtil(root.left, lmxh, lmnh) == false )
return false ;
if (isBalancedUtil(root.right, rmxh, rmnh) == false )
return false ;
maxh.d = Math.max(lmxh.d, rmxh.d) + 1;
minh.d = Math.min(lmnh.d, rmnh.d) + 1;
if (maxh.d <= 2 * minh.d)
return true ;
return false ;
}
function isBalanced(root) {
var maxh = new INT(), minh = new INT();
return isBalancedUtil(root, maxh, minh);
}
var root = new Node(10);
root.left = new Node(5);
root.right = new Node(100);
root.right.left = new Node(50);
root.right.right = new Node(150);
root.right.left.left = new Node(40);
document.write(isBalanced(root) ?
"Balanced" : "Not Balanced" );
</script>
|
Time Complexity: Time Complexity of above code is O(n) as the code does a simple tree traversal.
Auxiliary Space: O(n) for call stack since using recursion
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 Jul, 2022
Like Article
Save Article