32_3_剑指offer_从上到下打印二叉树 III

在这里插入图片描述

思路一:层序遍历的变式,辅助一个栈

从左到右采用层序遍历的思路,从右到左辅助一个栈。

public class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        // 之字形打印
        if (root == null) {
            return new LinkedList<>();
        }
        // 这个树不是空的
        Queue<TreeNode> queue = new LinkedList<>();
        boolean flag = true;//如果是true就从左到右,反之从右到左
        queue.add(root);
        List<List<Integer>> res = new LinkedList<>();
        while (!queue.isEmpty()) {
            int count = queue.size();
            List<Integer> list = new LinkedList<>();
            if (flag) {//应该从左到右打印
                for (int i = 0; i < count; i++) {
                    TreeNode tmp = queue.remove();
                    list.add(tmp.val);
                    if (tmp.left != null) queue.add(tmp.left);
                    if (tmp.right != null) queue.add(tmp.right);
                }
            } else {//应该从右到左打印
                Stack<Integer> stack = new Stack<>();
                for (int i = 0; i < count; i++) {
                    TreeNode tmp = queue.remove();
                    stack.push(tmp.val);
                    if (tmp.left != null) queue.add(tmp.left);
                    if (tmp.right != null) queue.add(tmp.right);
                }
                while (!stack.isEmpty()) {
                    list.add(stack.pop());
                }
            }
            res.add(list);
            flag = !flag;
        }
        return res;
    }
}

思路二:采用两个栈,比较巧妙。

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res;
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        stack1.push(root);
        int count = 1;
        while (!stack1.isEmpty() || !stack2.isEmpty()) {
            ArrayList<Integer> list = new ArrayList<>();
            if (count % 2 == 1) {//取出来,先存左儿子,再存右儿子
                while (!stack1.isEmpty()) {
                    TreeNode tmp = stack1.pop();
                    list.add(tmp.val);
                    if (tmp.left != null) stack2.push(tmp.left);
                    if (tmp.right != null) stack2.push(tmp.right);
                }
            } else {//取出来,先存右儿子,再存左儿子
                while (!stack2.isEmpty()) {
                    TreeNode tmp = stack2.pop();
                    list.add(tmp.val);
                    if (tmp.right != null) stack1.push(tmp.right);
                    if (tmp.left != null) stack1.push(tmp.left);
                }
            }
            count++;
            res.add(list);
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值