Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Day 22 Binary Search Trees 30 days of code solution

YASH PAL, 31 July 20249 February 2026

In this HackerRank Day 22 Binary Search Trees 30 days of code problem set, we need to complete a function getHeight that can take a pointer input and then print the height of the binary search tree.

HackerRank Day 22 Binary Search Trees 30 days of code solution

Problem solution in Python 2 programming.

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

Problem solution in Python 3 programming.  

    def getHeight(self,root):
        if root is None: 
            return -1 
        
        hL = self.getHeight(root.left)
        hR = self.getHeight(root.right)

        if hL > hR:
            return 1+hL
        else:
            return 1+hR     

Problem solution in java programming.

	public static int getHeight(Node root){
        if (root == null) return -1;
        return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
    }

Problem solution in c++ programming.

		int getHeight(Node* root){
          if (root==NULL) {
              return -1;
          } else {
              return 1 + std::max(getHeight(root->left),getHeight(root->right));
          }
        }

Problem solution in c programming.

int getHeight(Node* root){
  //Write your code here
    if (root==NULL)
        return 0;
    else if ((root->left==NULL) && (root->right==NULL))
        return 0;
    else if ((root->left != NULL) && (root->right==NULL))
        return 1+getHeight(root->left);
    else if ((root->left == NULL) && (root->right!=NULL))
        return 1+getHeight(root->right);
    else{
        int a, b;
        a=getHeight(root->left);
        b=getHeight(root->right);
        if(a>b)
            return 1 + a;
        else
            return 1 + b;
    }
}

Problem solution in Javascript programming.

        // Add your code here
        var height = 1;
        var height_left = -1;
        var height_right = -1;

        if (root.left)
            height_left = this.getHeight(root.left);
        
        if (root.right)
            height_right = this.getHeight(root.right);
        
        height += (height_left > height_right ? height_left : height_right);

        return height;

30 days of code coding problems solutions Hackerrank Problems Solutions HackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes