Skip to content
Programming101
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programmingoneonone

HackerRank Day 22 Binary Search Trees 30 days of code solution

YASH PAL, 31 July 2024

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

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes