30 Day code challenge-python

Ganesh Uthiravasagam
2 min readMar 29, 2021

--

Day 22— Binary Search Trees

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.

Sample input and output

Input Format

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

Editor will print the integer returned by your getHeight function denoting the height of the BST.

Solution

Explanation

Line 29–30: Gets input from the user and inserts value to the root node

Line 1–16: The values are inserted in the root node.

Line 35–36: Calls the getHeight function and prints the maximum height

Line 18–27: Returns the node which has the maximum value.

Bonus Tip: If the root is none, then, the value is considered as root value. If it’s greater than the root node then it’s sent to the right node of the root. If the value is smaller then it’s taken to the left node.

See you on day 23

--

--