# LeetCode | 面试题 04.08. 首个共同祖先【Python】

## 问题

``````    3
/ \
5   1
/ \ / \
6  2 0  8
/ \
7   4
``````

``````输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1

``````

``````输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4

``````

``````所有节点的值都是唯一的。
p、q 为不同节点且均存在于给定的二叉树中。
``````

## 思路

DFS

``````后序遍历分四种情况：
1. 左右子树都为空：左右子树都不包含p，q
2. 左子树为空：p，q 都不在左子树中
3. 右子树为空：p，q 都不在右子树中
4. 左右子树都不为空：p，q 分别在左右子树，当前 root 就是最近公共祖先节点
``````

## 代码

### Python3

``````# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
def lowestCommonAncestor(self, root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode:
if not root or root == p or root == q:
return root

left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)

# 左右子树都为空
if not left and not right:
return None
# 右子树为空
elif left and not right:
return left
# 左子树为空
elif not left and right:
return right
# 左右子树都不为空
else:
return root
``````

GitHub