LeetCode Top Interview Questions 103. Binary Tree Zigzag Level Order Traversal (Java版; Medium)

welcome to my blog

LeetCode Top Interview Questions 103. Binary Tree Zigzag Level Order Traversal (Java版; Medium)

题目描述
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, 
then right to left for the next level and alternate between).

For example:
Given binary tree [3,9,20,null,null,15,7],
    3
   / \
  9  20
    /  \
   15   7
return its zigzag level order traversal as:
[
  [3],
  [20,9],
  [15,7]
]
第一次做; 使用两个栈实现之字形打印; 不用统计个数和层数
/*
之字形打印二叉树, 使用两个栈就不用记录个数和层数了
*/
import java.util.List;
import java.util.ArrayList;
import java.util.Stack;


class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if(root==null)
            return res;
        ArrayList<Integer> tmp = new ArrayList<>();
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        stack1.push(root);
        TreeNode cur;
        while(!stack1.isEmpty() || !stack2.isEmpty()){
            while(!stack1.isEmpty()){
                cur = stack1.pop();
                tmp.add(cur.val);
                //左右
                if(cur.left!=null)
                    stack2.push(cur.left);
                if(cur.right!=null)
                    stack2.push(cur.right);
            }
            if(!tmp.isEmpty()){
                res.add(new ArrayList<Integer>(tmp));
                tmp.clear();
            }
            while(!stack2.isEmpty()){
                cur = stack2.pop();
                tmp.add(cur.val);
                //右左
                if(cur.right!=null)
                    stack1.push(cur.right);
                if(cur.left!=null)
                    stack1.push(cur.left);
            }
            if(!tmp.isEmpty()){
                res.add(new ArrayList<Integer>(tmp));
                tmp.clear();
            }
        }
        return res;
    }
}
第一次做; 仅适用一个队列(实际上是一个LinkedList,并不是严格的队列)实现, 利用了LinkedList可以操作队首和队尾的特性
细节:普通层序遍历用队列; zigzag遍历用两个栈? 可以; 也可以只用一个队列
*/
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;

class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<List<Integer>>();
        if(root==null)
            return res;
        // tmp记录当前层的节点val
        List<Integer> tmp = new ArrayList<>();
        // num记录下一层的节点个数; count表示当前层还有几个节点没有遍历
        int num=0, count=1;
        // 约定: flag==false表示当前层是奇数层; flag==true表示当前层是偶数层; root是奇数层
        boolean flag=false;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        TreeNode cur;
        while(!queue.isEmpty()){
            //奇数层:弹出队首元素; 左右孩子加入队尾
            if(flag==false){
                cur = queue.pollFirst();
                tmp.add(cur.val);
                if(cur.left!=null){
                    queue.addLast(cur.left);
                    num++;
                }
                if(cur.right!=null){
                    queue.addLast(cur.right);
                    num++;
                }
            }
            //偶数层:弹出队尾元素; 右左孩子加入队首
            else{
                cur = queue.pollLast();
                tmp.add(cur.val);
                if(cur.right!=null){
                    queue.addFirst(cur.right);
                    num++;
                }
                if(cur.left!=null){
                    queue.addFirst(cur.left);
                    num++;
                }
            }
            count--;
            if(count==0){
                count = num;
                num = 0;
                flag = !flag;
                res.add(new ArrayList<Integer>(tmp));
                tmp.clear();
            }
        }
        return res;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值