LeetCode. 236二叉树的最近公共祖先

88 阅读6分钟

题目描述

给定一颗二叉树,以及2个指定节点p , q,找到这2个节点的最近公共祖先。注意,一个节点也可以是它自己的祖先。并且根据提示,有p != q,并且pq均存在于二叉树中。

递归

由于pq一定存在于树中,则其最近公共祖先一定存在。其最近公共祖先有如下2种情况

  • 最近公共祖先是p或者q
  • 最近公共祖先不是p或者q

对于情况1,假设最近公共祖先是p,则q一定存在于p的左子树或右子树,此时直接返回p即可。当最近公共祖先是q时同理。

对于情况2,假设最近公共祖先是r,则pq一定分别位于r的左子树和右子树上。

由于求解的是这种节点的父子关系,很容易想到需要用DFS+回溯的方式。我们期望在DFS的过程中,能够把节点pq的信息,通过回溯传递上来。

所以我们这样来定义递归函数: 用dfs(node) 这个函数表示,对以node为根节点的二叉树进行搜索,若p在树中出现,则返回p;若q在树中出现,则返回q;若pq同时在树中出现,则返回pq的最近公共祖先。

伪代码为:

dfs(node):
    if (node == null) return null; // 跨过叶子节点
    if (node == p || node == q) return node; // 找到p或者q
    left = dfs(node.left); // 查找node的左子树, 看pq是否出现
    right = dfs(node.right); // 查找node的右子树, 看pq是否出现
    if (left != null && right != null) return node; // 当 pq 分别在 node 的左子树和右子树出现, 则最近公共祖先就是 node
    if (left != null) return left; // 若左子树上找到了pq, 返回左子树的查找结果 
    return right; // 右子树上找到了pq,返回右子树的查找结果

假设最近公共祖先不是pq,是r。并且假设p位于r左子树,q位于r右子树。则通过对r.left进行dfs搜索,能够把p的节点信息传递上来,对r.right进行dfs搜索,能够把q的节点信息传递上来。并且r节点,是唯一一个对其左子树进行dfs有结果,并且对其右子树进行dfs也有结果的节点。(r节点下面的节点,最多只可能在某一侧的dfs中有结果;r节点上面的节点,只会在r所在的这一侧的子树中,有dfs的结果)。

假设最近公共祖先是pq,则同样在遇到pq时,dfs函数就提前返回了,和本题的答案相一致。

注意该方法中,对递归函数dfs的定义,其实是带有扩展的,它求解的是:

  • p出现在二叉树中,则返回p
  • q出现,则返回q
  • pq同时出现,则返回二者的最近公共祖先
  • pq都不出现,返回null

而由于本题中pq一定存在于二叉树中,所以该方法能求出正确结果。

dfs函数在整个搜索过程中的作用是,将pq的信息,通过返回值,传递上来(从子节点,往上传递给父节点,回溯)。

代码如下:

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 root;
        return left != null ? left : right;
    }
}

哈希表

还有一种思路,由于我们要找的是公共祖先。那么我们可以先遍历一次,把所有节点的父节点存下来。然后对pq,依次往上寻找其父节点,把访问过的父节点存起来。先把p往上遍历,遍历完。随后开始遍历q,若在遍历q的父节点的过程中,出现了某个已经访问过的节点,则这个节点就是pq的公共祖先,且是最近的,因为是从下往上遍历。

获得了所有节点的父节点后,其实这道题就变成了 160. 相交链表 这道题了。

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        Map<TreeNode, TreeNode> parent = new HashMap<>();
        dfs(root, parent);
        // 相交链表
        Set<TreeNode> visited = new HashSet<>();
        while (p != null) {
            visited.add(p);
            p = parent.get(p);
        }
        while (q != null) {
            if (visited.contains(q)) return q;
            q = parent.get(q);
        }
        return null;
    }

    private void dfs(TreeNode root, Map<TreeNode, TreeNode> parent) {
        if (root == null) return;
        if (root.left != null) parent.put(root.left, root);
        if (root.right != null) parent.put(root.right, root);
        dfs(root.left, parent);
        dfs(root.right, parent);
    }
}

按相交链表这道题的一种巧妙的思路来做,如下

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        Map<TreeNode, TreeNode> parent = new HashMap<>();
        dfs(root, parent);
        // 相交链表
        TreeNode curP = p, curQ = q;
        while (curP != curQ) {
            curP = parent.get(curP) == null ? q : parent.get(curP);
            curQ = parent.get(curQ) == null ? p : parent.get(curQ);
        }
        return curP;
    }

    private void dfs(TreeNode root, Map<TreeNode, TreeNode> parent) {
        if (root == null) return;
        if (root.left != null) parent.put(root.left, root);
        if (root.right != null) parent.put(root.right, root);
        dfs(root.left, parent);
        dfs(root.right, parent);
    }
}

扩展

假设pq不一定都存在于二叉树当中,那么上面的方法就无法求出正确结果了。 我们换一种思路,我们设一个外部变量ans,来保存在dfs过程中可能搜到的答案,并且这样定义dfs函数: dfs(node)表示:在以node为根节点的二叉树中进行搜索,若树中出现了p或者q,则返回true,否则返回false

我们从根节点root开始进行DFS:

  • root等于pq时,只需要递归的搜索root的左子树和右子树,只要其中一侧的dfs返回了true,则说明找到了另一个节点,此时更新ans = root,然后提前返回即可(最近公共祖先是p或者q的情况
  • root不等于pq时,递归搜索两侧,只有当两侧的dfs都返回了true时,更新ans = root最近公共祖先不是p或者q的情况,此时的root节点,是唯一一个对其左右子树调用dfs都返回true的节点)
  • ans != null 时,说明已经找到答案,直接提前返回

另外要注意的是,假设root = p,且对root的子树调用dfs返回true,则说明在root子树中找到的一定是节点q;类似地,假设root不为p也不为q,若对root的左子树和右子树分别调用dfs都返回true,则一定是pq分别位于root的左右子树当中。 也就是说,当找到了一个节点时,对另外一边调用dfs如果返回true,则一定是找到了另外一个节点。(同一个节点不可能存在于一棵树上的两个位置)

这种解法能够在pq不存在于二叉树的情况下,返回正确结果(null)。

class Solution {
    TreeNode ans = null;
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        dfs(root, p, q);
        return ans;
    }

    private boolean dfs(TreeNode cur, TreeNode p, TreeNode q) {
        if (cur == null || ans != null) return false; // 跨过叶子节点, 或者已经找到答案, 直接返回
        if (cur == p || cur == q) {
            if (dfs(cur.left, p, q) || dfs(cur.right, p, q)) {
                ans = cur;
            }
            return true;
        }
        boolean left = dfs(cur.left, p, q);
        boolean right = dfs(cur.right, p, q);
        if (left && right) {
            ans = cur;
        }
        return left || right;
    }
}