【劍指Offer學習】【面試題58:二叉樹的下一個結點】,劍指offer

來源:互聯網
上載者:User

【劍指Offer學習】【面試題58:二叉樹的下一個結點】,劍指offer
題目:給定一棵二叉樹和其中的一個結點,如何找出中序遍曆順序的下一個結點?樹中的結點除了有兩個分別指向左右子結點的指標以外,還有一個指向父節點的指標。解題思路

  如果一個結點有右子樹,那麼它的下一個結點就是它的右子樹中的左子結點。也就是說右子結點出發一直沿著指向左子結點的指標,我們就能找到它的下一個結點。
  接著我們分析一個結點沒有右子樹的情形。如果結點是它父節點的左子結點,那麼它的下一個結點就是它的父結點。
  如果一個結點既沒有右子樹,並且它還是它父結點的右子結點,這種情形就比較複雜。我們可以沿著指向父節點的指標一直向上遍曆,直到找到一個是它父結點的左子結點的結點。如果這樣的結點存在,那麼這個結點的父結點就是我們要找的下一個結點。

結點定義
private static class BinaryTreeNode {    private int val;    private BinaryTreeNode left;    private BinaryTreeNode right;    private BinaryTreeNode parent;    public BinaryTreeNode() {    }    public BinaryTreeNode(int val) {        this.val = val;    }    @Override    public String toString() {        return val + "";    }}
代碼實現
public class Test58 {    private static class BinaryTreeNode {        private int val;        private BinaryTreeNode left;        private BinaryTreeNode right;        private BinaryTreeNode parent;        public BinaryTreeNode() {        }        public BinaryTreeNode(int val) {            this.val = val;        }        @Override        public String toString() {            return val + "";        }    }    public static BinaryTreeNode getNext(BinaryTreeNode node) {        if (node == null) {            return null;        }        // 儲存要尋找的下一個節點        BinaryTreeNode target = null;        if (node.right != null) {            target = node.right;            while (target.left != null) {                target = target.left;            }            return target;        } else if (node.parent != null){            target = node.parent;            BinaryTreeNode cur = node;            // 如果父新結點不為空白,並且,子結點不是父結點的左孩子            while (target != null && target.left != cur) {                cur = target;                target = target.parent;            }            return target;        }        return null;    }    private static void assemble(BinaryTreeNode node,                                 BinaryTreeNode left,                                 BinaryTreeNode right,                                 BinaryTreeNode parent) {        node.left = left;        node.right = right;        node.parent = parent;    }    public static void main(String[] args) {        test01();    }    //                            1    //                  2                   3    //             4         5          6          7    //          8     9   10   11   12   13    14   15    public static void test01() {        BinaryTreeNode n1 = new BinaryTreeNode(1); // 12        BinaryTreeNode n2 = new BinaryTreeNode(2); // 10        BinaryTreeNode n3 = new BinaryTreeNode(3); // 14        BinaryTreeNode n4 = new BinaryTreeNode(4); // 9        BinaryTreeNode n5 = new BinaryTreeNode(5); // 11        BinaryTreeNode n6 = new BinaryTreeNode(6); // 13        BinaryTreeNode n7 = new BinaryTreeNode(7); // 15        BinaryTreeNode n8 = new BinaryTreeNode(8); // 4        BinaryTreeNode n9 = new BinaryTreeNode(9); // 2        BinaryTreeNode n10 = new BinaryTreeNode(10); // 5        BinaryTreeNode n11 = new BinaryTreeNode(11); // 1        BinaryTreeNode n12 = new BinaryTreeNode(12); // 6        BinaryTreeNode n13 = new BinaryTreeNode(13); // 3        BinaryTreeNode n14 = new BinaryTreeNode(14); // 7        BinaryTreeNode n15 = new BinaryTreeNode(15); // null        assemble(n1, n2, n3, null);        assemble(n2, n4, n5, n1);        assemble(n3, n6, n7, n1);        assemble(n4, n8, n9, n2);        assemble(n5, n10, n11, n2);        assemble(n6, n12, n13, n3);        assemble(n7, n14, n15, n3);        assemble(n8, null, null, n4);        assemble(n9, null, null, n4);        assemble(n10, null, null, n5);        assemble(n11, null, null, n5);        assemble(n12, null, null, n6);        assemble(n13, null, null, n6);        assemble(n14, null, null, n7);        assemble(n15, null, null, n7);        System.out.println(getNext(n1));        System.out.println(getNext(n2));        System.out.println(getNext(n3));        System.out.println(getNext(n4));        System.out.println(getNext(n5));        System.out.println(getNext(n6));        System.out.println(getNext(n7));        System.out.println(getNext(n8));        System.out.println(getNext(n9));        System.out.println(getNext(n10));        System.out.println(getNext(n11));        System.out.println(getNext(n12));        System.out.println(getNext(n13));        System.out.println(getNext(n14));        System.out.println(getNext(n15));    }}
運行結果

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.