1. 程式人生 > >【劍指Offer學習】【面試題50:樹中兩個結點的最低公共祖先】

【劍指Offer學習】【面試題50:樹中兩個結點的最低公共祖先】

題目:求樹中兩個結點的最低公共祖先,此樹不是二叉樹,並且沒有指向父節點的指標。

樹的結點定義

private static class TreeNode {
    int val;

    List<TreeNode> children = new LinkedList<>();


    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    @Override
    public String toString() {
        return
val + ""; } }

題目解析

 假設還是輸入結點F和H .

這裡寫圖片描述

  我們首先得到一條從根結點到樹中某一結點的路徑,這就要求在遍歷的時候,有一個輔助記憶體來儲存路徑.比如我們用前序遍歷的方法來得到從根結點到H 的路徑的過程是這樣的:( 1 )遍歷到A,把A 存放到路徑中去,路徑中只有一個結點A; ( 2 )遍歷到B,把B 存到路徑中去,此時路徑為A->B; ( 3 )遍歷到D,把D 存放到路徑中去,此,時路徑為A->B->D; ( 4 ) :遍歷到F,把F 存放到路徑中去,此時路徑為A->B->D->F;( 5) F 已經沒有子結點了,因此這條路徑不可能到這結點H. 把F 從路徑中刪除,變成A->B->D; ( 6 )遍歷G. 和結點F 一樣,這條路徑也不能到達H. 邊歷完G 之後,路徑仍然是A->B->D; ( 7 )由於D 的所有子結點都遍歷過了,不可能到這結點H,因此D 不在從A 到H 的路徑中,把D 從路徑中刪除,變成A->B; ( 8 )遙歷E,把E 加入到路徑中,此時路徑變成A->B->E, ( 9 )遍歷H,已經到達目標給點, A->B->E 就是從根結點開始到達H 必須經過的路徑。
  同樣,我們也可以得到從根結點開始到達F 必須經過的路徑是A->B功。接著,我們求出這兩個路徑的最後公共結點,也就是B. B這個結點也是F 和H 的最低公共祖先.
  為了得到從根結點開始到輸入的兩個結點的兩條路徑,需要追歷兩次樹,每邊歷一次的時間複雜度是O(n).得到的兩條路徑的長度在最差情況時是0(時,通常情況丁兩條路徑的長度是O(logn).

注意:可以在只遍歷樹一次就找到兩個結點的路徑,這部分留給讀者自己去完成。

程式碼實現

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Test50 {
    /**
     * 樹的結點定義
     */
    private static class TreeNode {
        int val;

        List<TreeNode> children = new LinkedList<>();


        public
TreeNode() { } public TreeNode(int val) { this.val = val; } @Override public String toString() { return val + ""; } } /** * 找結點的路徑 * * @param root 根結點 * @param target 目標結點 * @param path 從根結點到目標結點的路徑 */ public static void getNodePath(TreeNode root, TreeNode target, List<TreeNode> path) { if (root == null) { return; } // 添加當前結點 path.add(root); List<TreeNode> children = root.children; // 處理子結點 for (TreeNode node : children) { if (node == target) { path.add(node); return; } else { getNodePath(node, target, path); } } // 現場還原 path.remove(path.size() - 1); } /** * 找兩個路徑中的最後一個共同的結點 * * @param p1 路徑1 * @param p2 路徑2 * @return 共同的結點,沒有返回null */ public static TreeNode getLastCommonNode(List<TreeNode> p1, List<TreeNode> p2) { Iterator<TreeNode> ite1 = p1.iterator(); Iterator<TreeNode> ite2 = p2.iterator(); TreeNode last = null; while (ite1.hasNext() && ite2.hasNext()) { TreeNode tmp = ite1.next(); if (tmp == ite2.next()) { last = tmp; } } return last; } /** * 找樹中兩個結點的最低公共祖先 * @param root 樹的根結點 * @param p1 結點1 * @param p2 結點2 * @return 公共結點,沒有返回null */ public static TreeNode getLastCommonParent(TreeNode root, TreeNode p1, TreeNode p2) { if (root == null || p1 == null || p2 == null) { return null; } List<TreeNode> path1 = new LinkedList<>(); getNodePath(root, p1, path1); List<TreeNode> path2 = new LinkedList<>(); getNodePath(root, p2, path2); return getLastCommonNode(path1, path2); } public static void main(String[] args) { test01(); System.out.println("=========="); test02(); System.out.println("=========="); test03(); } // 形狀普通的樹 // 1 // / \ // 2 3 // / \ // 4 5 // / \ / | \ // 6 7 8 9 10 public static void test01() { TreeNode n1 = new TreeNode(1); TreeNode n2 = new TreeNode(2); TreeNode n3 = new TreeNode(3); TreeNode n4 = new TreeNode(4); TreeNode n5 = new TreeNode(5); TreeNode n6 = new TreeNode(6); TreeNode n7 = new TreeNode(7); TreeNode n8 = new TreeNode(8); TreeNode n9 = new TreeNode(9); TreeNode n10 = new TreeNode(10); n1.children.add(n2); n1.children.add(n3); n2.children.add(n4); n4.children.add(n6); n4.children.add(n7); n3.children.add(n5); n5.children.add(n8); n5.children.add(n9); n5.children.add(n10); System.out.println(getLastCommonParent(n1, n6, n8)); } // 樹退化成一個連結串列 // 1 // / // 2 // / // 3 // / // 4 // / // 5 private static void test02() { TreeNode n1 = new TreeNode(1); TreeNode n2 = new TreeNode(2); TreeNode n3 = new TreeNode(3); TreeNode n4 = new TreeNode(4); TreeNode n5 = new TreeNode(5); n1.children.add(n2); n2.children.add(n3); n3.children.add(n4); n4.children.add(n5); System.out.println(getLastCommonParent(n1, n4, n5)); } // 樹退化成一個連結串列,一個結點不在樹中 // 1 // / // 2 // / // 3 // / // 4 // / // 5 private static void test03() { TreeNode n1 = new TreeNode(1); TreeNode n2 = new TreeNode(2); TreeNode n3 = new TreeNode(3); TreeNode n4 = new TreeNode(4); TreeNode n5 = new TreeNode(5); TreeNode n6 = new TreeNode(6); n1.children.add(n2); n2.children.add(n3); n3.children.add(n4); n4.children.add(n5); System.out.println(getLastCommonParent(n1, n5, n6)); } }

執行結果

這裡寫圖片描述