【劍指Offer學習】【面試題60:把二叉樹列印出多行】,劍指offer

來源:互聯網
上載者:User

【劍指Offer學習】【面試題60:把二叉樹列印出多行】,劍指offer
題目:從上到下按層列印二叉樹,同一層的結點按從左至右的順序列印,每一層列印一行。解題思路

  用一個隊列來儲存將要列印的結點。為了把二叉樹的每一行單獨列印到一行裡,我們需要兩個變數:一個變數表示在當前的層中還沒有列印的結點數,另一個變數表示下一次結點的數目。

結點定義
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 + "";    }}
代碼實現
import java.util.LinkedList;import java.util.List;public class Test60 {    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 + "";        }    }    /**     * 題目:從上到下按層列印二叉樹,同一層的結點按從左至右的順序列印,每一層列印一行。     * @param root     */    public static void print(BinaryTreeNode root) {        if (root == null) {            return;        }        List<BinaryTreeNode> list = new LinkedList<>();        BinaryTreeNode node;        // 當前層的結點個數        int current = 1;        // 記錄下一層的結點個數        int next = 0;        list.add(root);        while (list.size() > 0) {            node = list.remove(0);            current--;            System.out.printf("%-3d", node.val);            if (node.left != null) {                list.add(node.left);                next++;            }            if (node.right != null) {                list.add(node.right);                next++;            }            if (current ==0) {                System.out.println();                current = next;                next = 0;            }        }    }    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);    }}
運行結果

說明歡迎轉載,轉載請註明出處【http://blog.csdn.net/derrantcm/article/details/46857911】

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

聯繫我們

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