Hackerrank “30 Days of Code” Çözümleri – Day 22: Binary Search Trees

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 22: Binary Search Trees” sorusunun açıklaması ve çözümü. Bu soruda bir binary search tree‘nin yüksekliğini hesapladık.

► Hackerrank 30 Days of Code Çözümleri – Day 22: Binary Search Trees: https://www.hackerrank.com/challenges/30-binary-search-trees/problem

► Problem açıklaması:


Today, we’re working with Binary Search Trees (BSTs). Check out the Tutorial tab for learning materials and an instructional video!


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



The input forms the following BST:


The longest root-to-leaf path is shown below:


There are 4 nodes in this path that are connected by 3 edges, meaning our BST’s height = 3. Thus, we print 3 as our answer.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.