Added function for finding K-th smallest element in BST (#2318)

* fixes: #2172

* fixes: #2172

* Added docstrings and type of parameters

* fixed error

* Added type hints

* made changes

* removed capital letters from function name

* Added type hints

* fixed bulid error

* modified comments

* fixed build error
This commit is contained in:
SiddhantBobde 2020-08-21 12:25:50 +05:30 committed by GitHub
parent 0591968947
commit 0bf1f22d37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -141,6 +141,20 @@ class BinarySearchTree:
else: else:
return traversal_function(self.root) return traversal_function(self.root)
def inorder(self, arr: list, node: Node):
"""Perform an inorder traversal and append values of the nodes to
a list named arr"""
if node:
self.inorder(arr, node.left)
arr.append(node.value)
self.inorder(arr, node.right)
def find_kth_smallest(self, k: int, node: Node) -> int:
"""Return the kth smallest element in a binary search tree """
arr = []
self.inorder(arr, node) # append all values to list using inorder traversal
return arr[k - 1]
def postorder(curr_node): def postorder(curr_node):
""" """