Skip to content

Commit 5a8ce13

Browse files
committed
Added least common ancestor's code
1 parent ef6ede5 commit 5a8ce13

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

trees/least_common_ancestor.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ def insert(temp, data):
2929

3030

3131
def make_tree(elements):
32-
Tree = TreeNode(elements[0])
32+
tree = TreeNode(elements[0])
3333
for element in elements[1:]:
34-
insert(Tree, element)
35-
return Tree
34+
insert(tree, element)
35+
return tree
3636

3737

38-
class Solution(object):
39-
def lowestCommonAncestor(self, root, p, q):
38+
class Solution:
39+
def lowestcommonancestor(self, root, p, q):
4040
if not root:
4141
return None
42-
if root.data == p or root.data == q:
42+
if root.data in (p, q):
4343
return root
44-
left = self.lowestCommonAncestor(root.left, p, q)
45-
right = self.lowestCommonAncestor(root.right, p, q)
44+
left = self.lowestcommonancestor(root.left, p, q)
45+
right = self.lowestcommonancestor(root.right, p, q)
4646
if right and left:
4747
return root
4848
return right or left
4949

5050

5151
ob1 = Solution()
5252
tree = make_tree([3, 5, 1, 6, 2, 0, 8, None, None, 7, 4])
53-
print(ob1.lowestCommonAncestor(tree, 5, 1).data)
53+
print(ob1.lowestcommonancestor(tree, 5, 1).data)

0 commit comments

Comments
 (0)