25/180: How to determine if a binary tree is Height Balanced

  1. Left subtree of T is balanced
  2. Right subtree of T is balanced
  3. The difference between heights of left subtree and right subtree is not more than one. This above height balancing scheme is used in AVL trees.
  4. Get height of right and left tree. return true if difference between height is not more than one.
class BinaryTree{
Node root;
boolean isBalanced(Node node){
int lh;
int rh;
if(node == null)
return true;
lh = heigh(node.left);
rh = height(node.right);
if(Math.abs(lh -rh)<= 1 && isBalanced(node.left) && isBalanced(node.right))
return true;
return false;
}
int height(Node node){
if(node == null)
return 0;
return 1+Math.max(height(node.left), height(node.right)); }}

--

--

--

I am Indian by birth, Punjabi by destiny. Humanity is my religion. Love to eat, travel, read books and my million dreams keep me alive.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Averaging Vegetative Sentinel 2 Imagery with Python!

An Hour of Code during Computer Science Week

Queue Data Structure

Career Outlook as an Oracle Database Administrator

A Book Review

Designing a scalable ML Topic Model (NLP)

An Efficient Git Branching Strategy Every Developer Should Know

SOLID Principles with Examples in iOS & Swift (Part 2)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Navneet Ojha

Navneet Ojha

I am Indian by birth, Punjabi by destiny. Humanity is my religion. Love to eat, travel, read books and my million dreams keep me alive.

More from Medium

Amortized Time

0/1 Knapsack Problem

GREEDY ALGORITHMS

Sara Zielinski Interviews Lynn Umlauf Part 2