题目描述
Given the root of a binary tree, return the lowest common ancestor (LCA) of two given nodes, p and q. If either node p or q does not exist in the tree, return null. All values of the nodes in the tree are unique.
According to the definition of LCA on Wikipedia: “The lowest common ancestor of two nodes p and q in a binary tree T is the lowest node that has both p and q as descendants (where we allow a node to be a descendant of itself)”. A descendant of a node x is a node y that is on the path from node x to some leaf node.
样例
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 10
Output: null
Explanation: Node 10 does not exist in the tree, so return null.
Constraints:
The number of nodes in the tree is in the range [1, 104].
-109 <= Node.val <= 109
All Node.val are unique.
p != q
算法1
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def lowestCommonAncestor(self, root, p, q):
"""
:type root: TreeNode
:type p: TreeNode
:type q: TreeNode
:rtype: TreeNode
"""
parents = {root.val:None}
queue = collections.deque()
queue.append(root)
while queue:
size = len(queue)
for i in range(size):
node = queue.popleft()
if node.left:
parents[node.left.val] = node
queue.append(node.left)
if node.right:
parents[node.right.val] = node
queue.append(node.right)
ancestors = set()
if p.val not in parents or q.val not in parents:
return None
while p:
ancestors.add(p.val)
p = parents[p.val]
while q and q.val not in ancestors:
q = parents[q.val]
return q
算法2
def dfs(self, root, p, q):
if not root: return None
l = self.dfs(root.left, p, q)
r = self.dfs(root.right, p, q)
if root.val == p.val:
self.countp += 1
return root
if root.val == q.val:
self.countq += 1
return root
if l and r: return root
return l or r
def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode':
self.countp = 0
self.countq = 0
ans = self.dfs(root, p, q)
if not self.countp or not self.countq: return None
return ans