-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkth_largest_BST.py
More file actions
32 lines (27 loc) · 875 Bytes
/
kth_largest_BST.py
File metadata and controls
32 lines (27 loc) · 875 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class BinaryTree:
def __init__(self, root_node=None):
# Check out Use Me section to find out Node Structure
self.root = root_node
# Helper Method
def size(self, root):
if root == None:
return 0
else:
return (self.size(root.left_child) + 1 + self.size(root.right_child))
def list_nodes(self):
ordered_list = []
def traverse(root):
if root is None:
return
else:
traverse(root.left_child)
ordered_list.append(root)
traverse(root.right_child)
traverse(self.root)
return ordered_list
def find_kth_largest(self, root, k):
# Return Element should be of type TreeNode
if not root:
return None
list = self.list_nodes()
return list[-k]