Day 22 Binary Search Trees HackerRank Solution In C++

Problem:- day-22-binary-search-tree or Day 22 Binary search Tree hackerrank or Hacker Rank - Day 22 Binary search Tree or Hackerrank Day 22 Binary search Tree or BST(Binary search Tree) hackerrank or Day 22 BST(binary search tree) hackerrank solution or hackerrank Day 22 BST(binary search tree) solution or hackerrank Day 22 Binary search Tree solution or hackerrank solution BST (Binary Search Tree) solution or hackerrank solution binary search tree solution or hackerrank binary search tree solution

Binary Search Tree(BST):- I have explained everything about BST in next tutorial you can find a link to next tutorial at the end of this tutorial. 

1. Binary search Tree time complexity

Algorithm Average     Worst case
Space O(n)     O(n)
Search O(log n)     O(n)
Insert O(log n)     O(n)
Delete O(log n)     O(n)

Task:- The height of a binary search tree is the number of edges between the tree's root and its furthest leaf. You are given a pointer, root, pointing to the root of a binary search tree. Complete the getHeight function provided in your editor so that it returns the height of the binary search tree.

Input Format

  • The locked stub code in your editor reads the following inputs and assembles them into a binary search tree: 
  • The first line contains an integer, n, denoting the number of nodes in the tree. 
  • Each of the n subsequent lines contains an integer, data, denoting the value of an element that must be added to the BST.

Output Format

The locked stub code in your editor will print the integer returned by your getHeight function denoting the height of the BST.

Sample Input


Sample Output


Copy the colored code and paste it into Hacker rank editor and click to submit button, but before this process, you have to click the below link.

Submit this solution:- Click Here


#include <iostream>
#include <cstddef>

using namespace std;

class Node{
        int data;
        Node *left;
        Node *right;
        Node(int d){
            data = d;
            left = NULL;
            right = NULL;
class Solution{
  Node* insert(Node* root, int data) {
            if(root == NULL) {
                return new Node(data);
            else {
                Node* cur;
                if(data <= root->data){
                    cur = insert(root->left, data);
                    root->left = cur;
                    cur = insert(root->right, data);
                    root->right = cur;

               return root;
          int getHeight(Node* root){
          //Write your code here
          if(!root) {
              return -1;
          int leftDepth = getHeight(root->left);
          int rightDepth = getHeight(root->right);
          return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
      }; //End of Solution
int main() {
    Solution myTree;
    Node* root = NULL;
    int t;
    int data;

    cin >> t;

    while(t-- > 0){
        cin >> data;
        root = myTree.insert(root, data);
    int height = myTree.getHeight(root);
    cout << height;

    return 0;


Day 22 Binary Search Trees HackerRank Solution Output

You May Also See

1. Day 23: BST Level-Order Traversal

2. Day 24: More Linked Lists

3. Day 25: Running Time and Complexity

4. Day 26: Nested Logic

5. Day 27: Testing


  1. keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
    digital marketing training in bangalore | https://www.excelr.com/digital-marketing-training-in-bangalore