Leetcode103. 二叉树的锯齿形层次遍历(按之字顺序打印二叉树)

Binary Tree Zigzag Level Order Traversal
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]
]
循环在两个栈都为空时退出。循环体内只需要判断s1和s2哪个不为空
(每次新进入循环必定一个为空一个不为空),选择相应的分支进行下一层的遍历。
这里我们约定:mean针对0,2,4,...层,odd针对1,3,5,...层
注意奇偶层数的入栈顺序相反

List 的用法

  • list.addFirst(Object o) 在列表首部添加元素
  • list.addLast(Object o) 在列表末尾添加元素
  • list.get(int index):获取指定位置的元素
  • list.getFirst() 返回列表第一个元素
  • list.geLast() 返回列表最后一个元素
  • list.clear():删除集合中的所有元素
  • list.removeFirst() 删除列表中第一个元素
  • list.removeLast() 删除列表中最后一个元素
  • list.add() 添加元素
  • list.add(int index, E element)在指定位置添加元素
  • set(int index, E element):把指定索引位置的元素修改为指定的值,返回修改前的值

法一:用一个queue 然后奇数偶数层次分别考虑

根据层数的奇偶,来判断是否要反转temp

 class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> data =new ArrayList<>();
        Queue<TreeNode> que=new LinkedList<TreeNode>();
        if(root==null){
            return data;
        }
        que.add(root);
        int j=0;
        while(!que.isEmpty()){
            int size=que.size();
            List<Integer> res=new ArrayList<Integer>();
            for(int i=0;i<size;i++){
                TreeNode temp=que.poll();
                res.add(temp.val);
                if(temp.left!=null){
                    que.add(temp.left);
                }
                if(temp.right!=null){
                    que.add(temp.right);
                }
            }
            if(j%2==1){
                Collections.reverse(res);
            }
            data.add(res);
            j++;
        }
        return data;
    }
}
   

法二 用两个栈好麻烦

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>>result;//设结果数组
        if(root==NULL){
            return result;
        }
        stack<TreeNode*>mean;//为奇偶节点分别设置栈
        stack<TreeNode*>odd;
        mean.push(root);//首先0层节点入mean栈
        while(!mean.empty()||!odd.empty()){//循环中必有一栈不空
            vector<int>temp1;//为奇偶层节点值设temp12
            vector<int>temp2;
            while(!mean.empty()){//偶层不空,保存偶层所有数值并出栈,奇层进odd栈
                TreeNode*temp=mean.top();
                mean.pop();
                temp1.push_back(temp->val);
                if(temp->left!=NULL){
                    odd.push(temp->left);
                }
                if(temp->right!=NULL){
                    odd.push(temp->right);
                }
            }
            if(!temp1.empty()){//偶层节点数值
                result.push_back(temp1);
            }
            while(!odd.empty()){//奇层不空,保存奇层所有数值并出栈,偶层进mean栈
                TreeNode*temp=odd.top();
                odd.pop();
                temp2.push_back(temp->val);
                if(temp->right!=NULL){
                    mean.push(temp->right);
                }
                if(temp->left!=NULL){
                    mean.push(temp->left);
                }
            }
            if(!temp2.empty()){
                result.push_back(temp2);
            }
        }
        return result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值