mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 23:11:09 +00:00
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:
parent
0591968947
commit
0bf1f22d37
|
@ -141,6 +141,20 @@ class BinarySearchTree:
|
|||
else:
|
||||
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):
|
||||
"""
|
||||
|
|
Loading…
Reference in New Issue
Block a user