最近公共祖先系列

最近公共祖先I

描述:

給定一棵二叉樹,找到兩個(gè)節(jié)點(diǎn)的最近公共父節(jié)點(diǎn) (LCA)。最近公共祖先是兩個(gè)節(jié)點(diǎn)的公共的祖先節(jié)點(diǎn)且具有最大深度。

! 注意事項(xiàng) : 假設(shè)給出的兩個(gè)節(jié)點(diǎn)都在樹中存在

樣例 :

對于下面這棵二叉樹

  4
 / \
3   7
   / \
  5   6
輸出結(jié)果:
LCA(3, 5) = 4
LCA(5, 6) = 7
LCA(6, 7) = 7

代碼實(shí)現(xiàn):

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param root: The root of the binary search tree.
     * @param A and B: two nodes in a Binary.
     * @return: Return the least common ancestor(LCA) of the two nodes.
     */
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode A, TreeNode B) {
        if (root == null || root == A || root == B) {
            return root;
        }
        //divide and conquer
        TreeNode left = lowestCommonAncestor(root.left, A, B);
        TreeNode right = lowestCommonAncestor(root.right, A, B);
        if (left != null && right != null) {
            return root;
        }
        if (left != null) {
            return left;
        }
        if (left == null) {
            return right;
        }
        return null;
    }
}

最近公共祖先II

描述:

給一棵二叉樹和二叉樹中的兩個(gè)節(jié)點(diǎn),找到這兩個(gè)節(jié)點(diǎn)的最近公共祖先LCA。
兩個(gè)節(jié)點(diǎn)的最近公共祖先,是指兩個(gè)節(jié)點(diǎn)的所有父親節(jié)點(diǎn)中(包括這兩個(gè)節(jié)點(diǎn)),離這兩個(gè)節(jié)點(diǎn)最近的公共的節(jié)點(diǎn)。每個(gè)節(jié)點(diǎn)除了左右兒子指針以外,還包含一個(gè)父親指針parent,指向自己的父親。

樣例:

對于下面的這棵二叉樹

  4
 / \
3   7
   / \
  5   6
輸出結(jié)果:
LCA(3, 5) = 4
LCA(5, 6) = 7
LCA(6, 7) = 7

代碼實(shí)現(xiàn):

/**
 * Definition of ParentTreeNode:
 * 
 * class ParentTreeNode {
 *     public ParentTreeNode parent, left, right;
 * }
 */
public class Solution {
    /**
     * @param root: The root of the tree
     * @param A, B: Two node in the tree
     * @return: The lowest common ancestor of A and B
     */
    public ParentTreeNode lowestCommonAncestorII(ParentTreeNode root,
                                                 ParentTreeNode A,
                                                 ParentTreeNode B) {
        ArrayList<ParentTreeNode> pathA = getPath2Root(A);
        ArrayList<ParentTreeNode> pathB = getPath2Root(B);
        
        int indexA = pathA.size() - 1;
        int indexB = pathB.size() - 1;
        
        ParentTreeNode lowestAncestor = null;
        while (indexA >= 0 && indexB >= 0) {
            if (pathA.get(indexA) != pathB.get(indexB)) {
                break;
            }
            lowestAncestor = pathA.get(indexA);
            indexA--;
            indexB--;
        }
        
        return lowestAncestor;
    }
    
    private ArrayList<ParentTreeNode> getPath2Root(ParentTreeNode node) {
        ArrayList<ParentTreeNode> path = new ArrayList<>();
        while (node != null) {
            path.add(node);
            node = node.parent;
        }
        return path;

    }
}

最近公共祖先 III

描述:

給一棵二叉樹和二叉樹中的兩個(gè)節(jié)點(diǎn),找到這兩個(gè)節(jié)點(diǎn)的最近公共祖先 LCA。
兩個(gè)節(jié)點(diǎn)的最近公共祖先,是指兩個(gè)節(jié)點(diǎn)的所有父親節(jié)點(diǎn)中(包括這兩個(gè)節(jié)點(diǎn)),離這兩個(gè)節(jié)點(diǎn)最近的公共的節(jié)點(diǎn)。返回 null 如果兩個(gè)節(jié)點(diǎn)在這棵樹上不存在最近公共祖先的話。

樣例:

給出下面這棵樹:

  4
 / \
3   7
   / \
  5   6

輸出結(jié)果:
LCA(3, 5) = 4
LCA(5, 6) = 7
LCA(6, 7) = 7
LCA(5, 8) = null

代碼實(shí)現(xiàn):

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
 class ResultType {
    public boolean a_exist, b_exist;
    public TreeNode node;
    ResultType(boolean a, boolean b, TreeNode n) {
        a_exist = a;
        b_exist = b;
        node = n;
    }
}
public class Solution {
    /**
     * @param root The root of the binary tree.
     * @param A and B two nodes
     * @return: Return the LCA of the two nodes.
     */
    public TreeNode lowestCommonAncestor3(TreeNode root, TreeNode A, TreeNode B) {
        // write your code here
         // write your code here
        ResultType rt = helper(root, A, B);
        if (rt.a_exist && rt.b_exist)
            return rt.node;
        else
            return null;
    }

    public ResultType helper(TreeNode root, TreeNode A, TreeNode B) {
        if (root == null)
            return new ResultType(false, false, null);
            
        ResultType left_rt = helper(root.left, A, B);
        ResultType right_rt = helper(root.right, A, B);
        
        boolean a_exist = left_rt.a_exist || right_rt.a_exist || root == A;
        boolean b_exist = left_rt.b_exist || right_rt.b_exist || root == B;
        
        if (root == A || root == B)
            return new ResultType(a_exist, b_exist, root);

        if (left_rt.node != null && right_rt.node != null) 
            return new ResultType(a_exist, b_exist, root);
        if (left_rt.node != null)
            return new ResultType(a_exist, b_exist, left_rt.node);
        if (right_rt.node != null)
            return new ResultType(a_exist, b_exist, right_rt.node);

        return new ResultType(a_exist, b_exist, null);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 樹的概述 樹是一種非常常用的數(shù)據(jù)結(jié)構(gòu),樹與前面介紹的線性表,棧,隊(duì)列等線性結(jié)構(gòu)不同,樹是一種非線性結(jié)構(gòu) 1.樹的定...
    Jack921閱讀 4,764評論 1 31
  • 一. 尋找父親節(jié)點(diǎn)和孩子節(jié)點(diǎn) 首先需要回顧一下希爾伯特曲線的生成方式,具體代碼見筆者上篇文章的分析,在這個(gè)分析中,...
    一縷殤流化隱半邊冰霜閱讀 2,543評論 6 15
  • 題目 給定一棵二叉樹,找到兩個(gè)節(jié)點(diǎn)的最近公共父節(jié)點(diǎn)(LCA)。 最近公共祖先是兩個(gè)節(jié)點(diǎn)的公共的祖先節(jié)點(diǎn)且具有最大深...
    六尺帳篷閱讀 963評論 0 3
  • 描述 給一棵二叉樹和二叉樹中的兩個(gè)節(jié)點(diǎn),找到這兩個(gè)節(jié)點(diǎn)的最近公共祖先 LCA。兩個(gè)節(jié)點(diǎn)的最近公共祖先,是指兩個(gè)節(jié)點(diǎn)...
    6默默Welsh閱讀 597評論 0 0
  • 去看一場煙火吧,或者送自己一朵玫瑰花 你是陽光明媚的少年 你騎著自行車在校園里...
    YOUTH部落閱讀 576評論 3 10

友情鏈接更多精彩內(nèi)容