-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeast_common_ancestor.py
More file actions
27 lines (26 loc) · 879 Bytes
/
Least_common_ancestor.py
File metadata and controls
27 lines (26 loc) · 879 Bytes
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
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
from collections import namedtuple
class Solution:
# @param A : root node of tree
# @param B : integer
# @param C : integer
# @return an integer
def lca(self, A, B, C):
status = namedtuple('status',('number_target_nodes','ancestor'))
def rec(root,n1,n2):
if not root:
return status(0,None)
left = rec(root.left,n1,n2)
if left.number_target_nodes == 2:
return left
right = rec(root.right,n1,n2)
if right.number_target_nodes == 2:
return right
num = (left.number_target_nodes + right.number_target_nodes + int(root.val == n1) + int(root.val == n2))
return status(num,root.val if num==2 else -1)
return rec(A,B,C).ancestor