【劍指Offer學習】【面試題63:二叉搜尋樹的第k個結點】,劍指offer

來源:互聯網
上載者:User

【劍指Offer學習】【面試題63:二叉搜尋樹的第k個結點】,劍指offer
題目:給定一棵二叉搜尋樹,請找出其中的第k大的結點。解題思路

  如果按照中序遍曆的順序遍曆一棵二叉搜尋樹,遍曆序列的數值是遞增排序的。只需要用中序遍曆演算法遍曆一棵二叉搜尋樹,就很容易找出它的第k大結點。

結點定義
private static class BinaryTreeNode {    private int val;    private BinaryTreeNode left;    private BinaryTreeNode right;    public BinaryTreeNode() {    }    public BinaryTreeNode(int val) {        this.val = val;    }    @Override    public String toString() {        return val + "";    }}
代碼實現
public class Test63 {    private static class BinaryTreeNode {        private int val;        private BinaryTreeNode left;        private BinaryTreeNode right;        public BinaryTreeNode() {        }        public BinaryTreeNode(int val) {            this.val = val;        }        @Override        public String toString() {            return val + "";        }    }    public static BinaryTreeNode kthNode(BinaryTreeNode root, int k) {        if (root == null || k < 1) {            return null;        }        int[] tmp = {k};        return kthNodeCore(root, tmp);    }    private static BinaryTreeNode kthNodeCore(BinaryTreeNode root, int[] k) {        BinaryTreeNode result = null;        // 先成左子樹中找        if (root.left != null) {          result =  kthNodeCore(root.left, k);        }        // 如果在左子樹中沒有找到        if (result == null) {            // 說明當前的根結點是所要找的結點            if(k[0] == 1) {                result = root;            } else {                // 當前的根結點不是要找的結點,但是已經找過了,所以計數器減一                k[0]--;            }        }        // 根結點以及根結點的右子結點都沒有找到,則找其右子樹        if (result == null && root.right != null) {            result = kthNodeCore(root.right, k);        }        return result;    }    public static void main(String[] args) {        BinaryTreeNode n1 = new BinaryTreeNode(1);        BinaryTreeNode n2 = new BinaryTreeNode(2);        BinaryTreeNode n3 = new BinaryTreeNode(3);        BinaryTreeNode n4 = new BinaryTreeNode(4);        BinaryTreeNode n5 = new BinaryTreeNode(5);        BinaryTreeNode n6 = new BinaryTreeNode(6);        BinaryTreeNode n7 = new BinaryTreeNode(7);        BinaryTreeNode n8 = new BinaryTreeNode(8);        BinaryTreeNode n9 = new BinaryTreeNode(9);        n1.left = n2;        n1.right = n3;        n2.left = n4;        n2.right = n5;        n3.left = n6;        n3.right = n7;        n4.left = n8;        n4.right = n9;        print(n1);        System.out.println();        for (int i = 0; i <= 10; i++) {            System.out.printf(kthNode(n1, i) + ", ");        }    }    /**     * 中序遍曆一棵樹     * @param root     */    private static void print(BinaryTreeNode root) {        if (root != null) {            print(root.left);            System.out.printf("%-3d", root.val);            print(root.right);        }    }}
運行結果

特別說明歡迎轉載,轉載請註明出處【http://blog.csdn.net/DERRANTCM/article/details/46872313】

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

聯繫我們

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