Update and rename lowest_common_ancestor.py to bt_lowest_common_ancestor.py

This commit is contained in:
marina 2023-08-03 13:18:31 -07:00 committed by GitHub
parent ca5c15c9b6
commit 7520042ef9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,27 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
class Tree:
def lowest_common_ancestor(self, root, p, q):
def dfs(root, p, q):
if not root:
return False
left = dfs(root.left, p, q)
right = dfs(root.right, p, q)
mid = root == p or root == q
if mid + left + right >= 2:
self.answer = root
return left or right or mid
dfs(root, p, q)
return self.answer