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

來源:互聯網
上載者:User

【劍指Offer學習】【面試題50:樹中兩個結點的最低公用祖先】,劍指offer
題目:求樹中兩個結點的最低公用祖先,此樹不是二叉樹,並且沒有指向父節點的指標。樹的結點定義

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));    }}
運行結果

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.