so I am doing a Find Kth Largest Value in BST leetcode problem and I want to update the k_count every time I iterate through the tree. As you can see I print the tree value and the k_count everytime I go through. It works in that I am returning the value of the tree from largest value to smallest but I want the assocaited K_count with each tree node to update respectively. That is I want the largest value to have a k_count of 1 the second largest to have a k_count of 2 the third largest a k_count of 3 etc. I know I am close but getting thrown off with the recursive stack.
Any help with this would be greatly appreciated!
# This is an input class. Do not edit.
class BST:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def findKthLargestValueInBst(tree, k):
k_count = 0
result = 0
return findhelper(tree, k, k_count, result)
def findhelper(tree, k, k_count, result):
if tree is not None:
findhelper(tree.right, k, k_count, result)
print(tree.value)
print(k_count)
if k_count == k:
result = tree.value
findhelper(tree.left, k, k_count+1, result)
return result