算法树中两个结点的最低公共祖结点

Posted Kant101

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了算法树中两个结点的最低公共祖结点相关的知识,希望对你有一定的参考价值。

1. 概述

树的最低公共祖结点,即从根结点开始到两个给定的结点,会形成两条对应的路径,即这两条路径最低的交汇点。

如下图所示的树,8结点和11结点的最低公共祖结点是5结点。


2. 思路

思路:
1)提供一个方法,分别从根结点开始向下查找到对应的给定结点,得到对应的路径;

2)从上面得到的两条路径中,查找到最近的交汇点。
从上图可以得到两条路径的第一个交汇点为结点5,即为最低公共祖结点


3. 代码及测试

Java

package cn.pku.edu.algorithm.leetcode.plus;

import cn.pku.edu.algorithm.leetcode.common.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author allen
 * @date 2022/9/19
 */
public class LastCommonParent 
    public static void main(String[] args) 
        LastCommonParent lastCommonParent = new LastCommonParent();
        TreeNode root = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode5 = new TreeNode(5);
        TreeNode treeNode6 = new TreeNode(6);
        TreeNode treeNode7 = new TreeNode(7);
        TreeNode treeNode8 = new TreeNode(8);
        TreeNode treeNode9 = new TreeNode(9);
        TreeNode treeNode10 = new TreeNode(10);
        TreeNode treeNode11 = new TreeNode(11);

        root.left = treeNode2;
        root.right = treeNode3;

        treeNode2.left = treeNode4;
        treeNode2.right = treeNode5;

        treeNode3.right = treeNode6;

        treeNode4.left = treeNode7;

        treeNode5.left = treeNode8;
        treeNode5.right = treeNode9;

        treeNode8.left = treeNode10;

        treeNode9.right = treeNode11;

        TreeNode last = lastCommonParent.getLastCommonParent(root, treeNode8, treeNode11);
        if (last != null) 
            System.out.println(last.val);
         else 
            System.out.println("last common parent is null");
        
    

    public TreeNode getLastCommonParent(TreeNode root, TreeNode node1, TreeNode node2) 
        if (root == null || node1 == null || node2 == null) 
            return null;
        
        List<TreeNode> path1 = new ArrayList<>();
        getNodePath(root, node1, path1);

        List<TreeNode> path2 = new ArrayList<>();
        getNodePath(root, node2, path2);

        return getLastCommonNode(path1, path2);
    

    private boolean getNodePath(TreeNode root, TreeNode node, List<TreeNode> path) 
        // 到达了指定结点
        if (root == node) 
            path.add(root);
            return true;
        
        path.add(root);
        boolean found = false;
        if (root.left != null) 
            found = getNodePath(root.left, node, path);
        
        if (!found && root.right != null) 
            found = getNodePath(root.right, node, path);
        
        // 以本结点为根节点的子树中未找到指定结点,则将本结点从路径中去掉
        if (!found) 
            path.remove(path.size() - 1);
        

        return found;
    

    private TreeNode getLastCommonNode(List<TreeNode> path1, List<TreeNode> path2) 
        int n = path1.size();
        TreeNode last = null;
        for (int i = 0; i < n; i++) 
            TreeNode node1 = path1.get(i);
            TreeNode node2 = path2.get(i);
            if (node1 != node2) 
                break;
            
            last = node1;
        
        return last;
    

以上是关于算法树中两个结点的最低公共祖结点的主要内容,如果未能解决你的问题,请参考以下文章

求解二叉树中两个结点的最低公共父结点

二叉树中找两个结点的最近公共祖先结点

二叉树中找两个结点的最近公共祖先结点

寻找二叉树中的最低公共祖先结点----LCA(Lowest Common Ancestor )问题(递归)

剑指Offer对答如流系列 - 树中两个结点的最低公共祖先

剑指Offer打卡day43—— ACWing 88. 树中两个结点的最低公共祖先