【劍指Offer學習】【面試題61:按之字形順序列印二叉樹】,劍指offer

來源:互聯網
上載者:User

【劍指Offer學習】【面試題61:按之字形順序列印二叉樹】,劍指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 Test61 {    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 void print(BinaryTreeNode root) {        if (root == null) {            return;        }        List<BinaryTreeNode> current = new LinkedList<>();        List<BinaryTreeNode> reverse = new LinkedList<>();        int flag = 0;        BinaryTreeNode node;        current.add(root);        while (current.size() > 0) {            // 從最後一個開始取            node = current.remove(current.size() - 1);            System.out.printf("%-3d", node.val);            // 當前是從左往右列印的,那就按從左往右入棧            if (flag == 0) {                if (node.left != null) {                    reverse.add(node.left);                }                if (node.right != null) {                    reverse.add(node.right);                }            }            // 當前是從右往左列印的,那就按從右往左入棧            else {                if (node.right != null) {                    reverse.add(node.right);                }                if (node.left != null) {                    reverse.add(node.left);                }            }            if (current.size() == 0) {                flag = 1 - flag;                List<BinaryTreeNode> tmp = current;                current = reverse;                reverse = tmp;                System.out.println();            }        }    }    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/46857935】

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

聯繫我們

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