-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1123. Lowest Common Ancestor of Deepest Leaves.py
51 lines (51 loc) · 1.94 KB
/
1123. Lowest Common Ancestor of Deepest Leaves.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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from collections import deque as queue
class Solution:
def lcaDeepestLeaves(self, root: TreeNode) -> TreeNode:
if root is None:
return
q=queue()
q.append(root)
q.append(None)
l,a=[],[]
while len(q)>0:
curr=q.popleft()
if curr is None:
l.append(a)
a=[]
if len(q)==0:
break
q.append(None)
else:
if curr.left:
q.append(curr.left)
if curr.right:
q.append(curr.right)
a.append(curr.val)
#print(l)
if len(l[-1])==1:
return TreeNode(l[-1][0])
node = self.lowestCommonAncestor(root,l[-1][0],l[-1][1])
#instead of calculating lca of all nodes, just calculate lca of first and last node of last level
#for i in range(2,len(l[-1])):
# node=self.lowestCommonAncestor(root,node.val,l[-1][i])
#return node
return self.lowestCommonAncestor(root,l[-1][0],l[-1][len(l[-1])-1])
def lowestCommonAncestor(self,root,p,q):
if root is None:
return None
if root.val==p or root.val==q:
return root
l=self.lowestCommonAncestor(root.left,p,q)
r=self.lowestCommonAncestor(root.right,p,q)
if l is not None and r is not None:
return root
if l is None and r is None:
return None
return l if l is not None else r