Learn basic concept of c , c++ and python programming in regularcodes.com

Spread the post

Binary search find k'th smallest element

In this post we are learning about how to find Binary tree or binary search tree kth smallest element. Assume that following data are inserted on tree [89, 79, 150, 56, 654, 43, 78, 437, 50, 47].

Binary search tree find kth smallest element

Try it yourself

4th smallest element of this tree.

Example 4th smallest element of BST

7th smallest element of this tree.

Example 7th smallest element of BST

Algorithm:

Time complexity of this program O(n)

Accepted Output

Given BST tree node is:

50, 70, 30, 90, 20, 10, 15, 60, 80

find smallest element [3]

Result : [3] Smallest element is : [20]

find smallest element [7]

[7] Smallest element is : [70]

find smallest element [2]

Result : [2] Smallest element is : [15]

find smallest element [-1]

Result : [-1] Invalid Smallest element

find smallest element [12]

Result : [12] Smallest element not exist

Code execution

C program to find kth smallest binary tree nodes or BST nodes. Recursive approach.

Output
 BST Data is :  43  47  50  56  78  79  89  150  437  654
 Invalid small element [-1]

 [3] Smallest element is :  [50]

 [5] Smallest element is :  [78]

 [9] Smallest element is :  [437]

 Given smallest element [11] not exist

Spread the post

Recommended Posts: