-
Notifications
You must be signed in to change notification settings - Fork 617
/
LCA_in_binary_tree.py
114 lines (102 loc) · 2.12 KB
/
LCA_in_binary_tree.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
'''
PROBLEM STATEMENT:
Given a binary tree with its root node and two nodes, a and b, present in
the tree. The task is to find the lowest common ancestor of a and b.
The input is in preorder and -1 denotes a null value.
For example:
Input: 3 4 -1 6 -1 -1 5 1 -1 -1 -1
let a = 1, b = 6
The above input will have the following structure:
3
/ \
4 5
\ /
6 1
Output: 3, as 3 is the lowest common ancestor of 1 and 6
'''
# A class to create a node
class Node:
# Constructor to initialize node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
# A function to builfd the tree in preorder form
def BuildTree():
d = int(input())
if d == -1:
return None
root = Node(d)
root.left = BuildTree()
root.right = BuildTree()
return root
# A function to find lowest common ancestor of the two entered nodes
def LCA(root, a, b):
# base case
if root is None:
return None
if root.data == a or root.data == b:
return root
# Recursive calls
leftans = LCA(root.left, a, b)
rightans = LCA(root.right, a, b)
if leftans is not None and rightans is not None:
return root
elif leftans is not None:
return leftans
else:
return rightans
print("Enter values in a binary tree:")
# A function call to build the tree and return root node
root = BuildTree()
print("Enter a and b:")
# Assuming a and b are present in the tree
a = int(input())
b = int(input())
# CA stores the lowest common ancestor of a and b
CA = LCA(root, a, b)
print("LCA of", a, "and", b, "is", CA.data)
'''
TEST CASES:
1.
Input:
Enter values in a binary tree:
3
4
-1
6
-1
-1
5
1
-1
-1
-1
Enter a and b:
3
6
Output: LCA of 3 and 6 is 3
2.
Input:
Enter values in a binary tree:
2
4
6
-1
7
-1
-1
3
-1
-1
5
-1
-1
Enter a and b:
5
7
Output: LCA of 5 and 7 is 2
TIME COMPLEXITY: O(n), due to a single traversal of the tree
where, 'n' denotes the number of nodes in the tree.
SPACE COMPLEXITY: O(1), no extra space used.
'''