All problems
BST Height
Problem
Return the height of a binary search tree built from the given values, measured in nodes.
Insert the values in order (duplicates ignored). The height is the number of nodes on the longest path from the root to a leaf: an empty tree has height 0 and a single node has height 1.
Return the height.
Hint
Track the depth of each node during a DFS and keep the maximum.
Test cases
case 1
in: [[5,3,8,1]]
expected: 3
case 2
in: [[5]]
expected: 1
case 3
in: [[]]
expected: 0
case 4
in: [[10,5,15,3,7,12,20]]
expected: 3
Starter codePython
def bst_height(values):
# Your code here
pass4 test cases · run your code against them in the editor