leetbook_ioa/docs/LCR 194. 寻找二叉树的最近公共祖先.md
祖先的定义: 若节点 p 在节点 root 的左(右)子树中,或 p = root ,则称 root 是 p 的祖先。
最近公共祖先的定义: 设节点 root 为节点 p , q 的某公共祖先,若其左子节点 root.left 和右子节点 root.right 都不是 p , q 的公共祖先,则称 root 是 “最近的公共祖先” 。
{:align=center width=450}
根据以上定义,若 root 是 p , q 的 最近公共祖先 ,则只可能为以下情况之一:
p 和 q 在 root 的子树中,且分列 root 的 异侧(即分别在左、右子树中);p = root ,且 q 在 root 的左或右子树中;q = root ,且 p 在 root 的左或右子树中;{:align=center width=450}
考虑通过递归对二叉树进行先序遍历,当遇到节点 p 或 q 时返回。从底至顶回溯,当节点 p , q 在节点 root 的异侧时,节点 root 即为最近公共祖先,则向上返回 root 。
root 等于 p , q ,则直接返回 root ;left ;right ;left 和 right ,可展开为四种情况;
left 和 right 同时为空 :说明 root 的左 / 右子树中都不包含 p , q ,返回 $\text{null}$ ;left 和 right 同时不为空 :说明 p , q 分列在 root 的 异侧 (分别在 左 / 右子树),因此 root 为最近公共祖先,返回 root ;left 为空 ,right 不为空 :p , q 都不在 root 的左子树中,直接返回 right 。具体可分为两种情况:
p , q 其中一个在 root 的 右子树 中,此时 right 指向 p(假设为 p );p , q 两节点都在 root 的 右子树 中,此时的 right 指向 最近公共祖先节点 ;left 不为空 ,right 为空 :与情况 3. 同理;观察发现,情况
1.可合并至3.和4.内,详见文章末尾代码。
<,,,,,,,,,,,,,,,,,>
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: return right
if not right: return left
return root
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q) return root;
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if(left == null) return right;
if(right == null) return left;
return root;
}
}
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == nullptr || root == p || root == q) return root;
TreeNode *left = lowestCommonAncestor(root->left, p, q);
TreeNode *right = lowestCommonAncestor(root->right, p, q);
if(left == nullptr) return right;
if(right == nullptr) return left;
return root;
}
};
情况 1. , 2. , 3. , 4. 的展开写法如下。
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 # 1.
if not left: return right # 3.
if not right: return left # 4.
return root # 2. if left and right:
class Solution {
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if(root == null || root == p || root == q) return root;
TreeNode left = lowestCommonAncestor(root.left, p, q);
TreeNode right = lowestCommonAncestor(root.right, p, q);
if(left == null && right == null) return null; // 1.
if(left == null) return right; // 3.
if(right == null) return left; // 4.
return root; // 2. if(left != null and right != null)
}
}
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == nullptr || root == p || root == q) return root;
TreeNode *left = lowestCommonAncestor(root->left, p, q);
TreeNode *right = lowestCommonAncestor(root->right, p, q);
if(left == nullptr && right == nullptr) return nullptr; // 1.
if(left == nullptr) return right; // 3.
if(right == nullptr) return left; // 4.
return root; // 2. if(left != null and right != null)
}
};