• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

Program to Find the Height of the Binary Tree using Recursion

Aniruddha Chaudhari/11655/0
CodeCSE SubjectData StructurePython

What is the Height of Binary Tree (BT)?

The height of the Binary Tree is the number of edges from the root node to the deepest leaf node.

reorder, inorder and post order traversal

The height of the tree is the same as the height of the root node.

In the above binary tree,

  • The height of the tree (height of the root node) is 2.
  • The height of the node 5 is one.

What is the maximum and minimum height of the binary tree having n elements?

The height of the binary tree can always be in the range of log(n) to (n-1).

  • If the BT is fully balanced (every node has zero or two nodes), the height of the tree is log(n).
  • If every node in the binary tree has only one node attached, the height of the BT is (n-1).

For example, the binary tree having eight nodes can have minimum height log(8)=3 and maximum height 8-1=7 nodes.

You can find the height of the binary tree using recursion technique.

Using recursion, it is simple. The height of any node (root) is one plus maximum of the height of the left and right node.

Python Program to find the Height of the Binary Tree:

class node:
    def __init__(self, val, left=None, right=None):
        self.left=left
        self.val=val
        self.right=right

def height(root):
    if root:
        return 1+max(height(root.left), height(root.right))
    else:
        return -1

root=node(4)
root.left=node(1)
root.right=node(5)
root.left.left=node(7)
root.left.right=node(3)
root.right.right=node(6)

print("Height of the Binary Tree: ", height(root))

Output:

Height of the Binary Tree:  2

If there is no right or left child to the root node, the height of the BT will be zero.

You can write a program to find the height of the Binary Tree in any programming languages like C/C++, Java using the same logic.

Knowing the height of the binary tree is very important to solve many of the binary tree problems in competitive programming.

algorithmBinary Tree
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Binary Tree (BT)

  1. BT- Introduction
  2. BT- Create Binary Tree
  3. BT- Pre/In/Post Order Traversal
  4. BT- Height 
  5. BT- Level Order Traversal
  6. BT- Min and Max Heap
  7. BT- Parent of a Given Node
  8. BT- Ancestors of Given Node
  9. BT- Lowest Common Ancestor
  10. BT- Check Sub Tree

Binary Search Tree (BST)

  1. BST- Introduction
  2. BST- Search Node
  3. BST- Insert Node

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard