剑指offer五十九之按之字形顺序打印二叉树

一、题目

  请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
二、思路

  详见代码

三、代码

import java.util.ArrayList;
import java.util.Stack;

public class Solution {
    public  ArrayList<ArrayList<Integer>> print(TreeNode pRoot) {
        int layer = 1;
        //s1存奇数层节点
        Stack<TreeNode> s1 = new Stack<>();
        s1.push(pRoot);
        //s2存偶数层节点
        Stack<TreeNode> s2 = new Stack<>();

        //返回结果
        ArrayList<ArrayList<Integer>> list = new ArrayList<>();

        while (!s1.empty() || !s2.empty()) {
            if (layer % 2 != 0) {
                ArrayList<Integer> temp = new ArrayList<>();
                while (!s1.empty()) {
                    TreeNode node = s1.pop();
                    if (node != null) {
                        temp.add(node.val);
                        System.out.print(node.val + " ");
                        s2.push(node.left);
                        s2.push(node.right);
                    }
                }
                if (!temp.isEmpty()) {
                    list.add(temp);
                    layer++;
                    System.out.println();
                }
            } else {
                ArrayList<Integer> temp = new ArrayList<>();
                while (!s2.empty()) {
                    TreeNode node = s2.pop();
                    if (node != null) {
                        temp.add(node.val);
                        System.out.print(node.val + " ");
                        s1.push(node.right);
                        s1.push(node.left);
                    }
                }
                if (!temp.isEmpty()) {
                    list.add(temp);
                    layer++;
                    System.out.println();
                }
            }
        }
        return list;
    }
}
View Code

相关测试函数

public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
View Code
import java.util.ArrayList;

public class TestMain {
    public static void main(String[] args) {
        //注意必须逆序建立,先建立子节点,再逆序往上建立,因为非叶子结点会使用到下面的节点,而初始化是按顺序初始化的,不逆序建立会报错
       TreeNode G = new TreeNode(1, null, null);
       TreeNode F = new TreeNode(2, null, null);
       TreeNode E = new TreeNode(3, null, null);
       TreeNode D = new TreeNode(4, null, null);
       TreeNode C = new TreeNode(5, F, G);
       TreeNode B = new TreeNode(6, D, E);
       TreeNode A = new TreeNode(8, B, C);

        ArrayList<ArrayList<Integer>>  arrayList=new ArrayList<>();

        Solution s=new Solution();
        arrayList= s.print(A);

        //之字形遍历输出
        for(int i=0;i<arrayList.size();i++){
            for (int j=0;j<arrayList.get(i).size();j++){
                System.out.print(arrayList.get(i).get(j)+",");
            }
            System.out.println();
        }

    }
}
View Code

---------------------------------------------

参考链接:

https://www.nowcoder.com/questionTerminal/91b69814117f4e8097390d107d2efbe0

posted @ 2017-10-22 13:57  AI菌  阅读(199)  评论(0编辑  收藏  举报