forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kth_Ancester.py
37 lines (31 loc) · 856 Bytes
/
Kth_Ancester.py
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
33
34
35
36
37
class TreeNode:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def kth_ancestor(root, node, k):
if root is None:
return None
if root.key == node:
return k
left_k = kth_ancestor(root.left, node, k)
if left_k is not None:
if left_k == 0:
print("K-th ancestor:", root.key)
return left_k - 1
right_k = kth_ancestor(root.right, node, k)
if right_k is not None:
if right_k == 0:
print("K-th ancestor:", root.key)
return right_k - 1
# Example usage:
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.right.left = TreeNode(6)
root.right.right = TreeNode(7)
k = 2
node_to_find = 7
kth_ancestor(root, node_to_find, k)