Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Aug 21, 2020
12 changes: 12 additions & 0 deletions data_structures/binary_tree/binary_search_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ def traversal_tree(self, traversal_function=None):
else:
return traversal_function(self.root)

def inorder(self, arr, Node):
if Node:
self.inorder(arr, Node.left)
arr.append(Node.value)
self.inorder(arr, Node.right)

def find_Kth_Smallest(self, k, Node):
arr = []
self.inorder(arr, Node)
arr.sort()
return arr[k - 1]


def postorder(curr_node):
"""
Expand Down