leetcode每日一题:103. 二叉树的锯齿形层序遍历

一、题目描述

给定一个二叉树,返回其节点值的锯齿形层序遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
例如:
给定二叉树 [3,9,20,null,null,15,7],
3
/
9 20
/
15 7
返回锯齿形层序遍历如下:
[
[3],
[20,9],
[15,7]
]

二、题解
方法一:一个队列+数组的反转

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null) return res;
        Queue<TreeNode> workQueue = new LinkedList<>();
        workQueue.add(root);
        int levelNum = 1;//该层的结点数目
        int levelCount = 0;//计数
        int level = 1;//层数
        TreeNode p;
        while(!workQueue.isEmpty()){
            levelNum = workQueue.size();
            List<Integer> tempList = new ArrayList<>();
            while(levelCount<levelNum){
                p = workQueue.poll();
                tempList.add(p.val);
                levelCount++;
                if(p.left!=null)
                    workQueue.add(p.left);
                if(p.right!=null)
                    workQueue.add(p.right);
            }
            levelCount = 0;
            //如果是从右往左读,那么就反转该列表
            if(level %2 == 0){
                Collections.reverse(tempList);
            }
            res.add(tempList);
            level++;
        }
        return res;
    }
}

方法二:一个队列+一个栈
先入栈,然后根据读的顺序不同选择是先左孩子入队列还是先右孩子入队列

class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> res;
        if(root == nullptr)
            return res;
        vector<int> tempRes;
        queue<TreeNode*> workQueue;
        stack<TreeNode*> workStack;
        workQueue.push(root);
        int level = 1;
        int levelNum = 0;
        int levelCount = 0;
        TreeNode* p;
        while(!workQueue.empty()){
            levelNum = workQueue.size();
            while(levelCount<levelNum){
                p = workQueue.front();
                workQueue.pop();
                tempRes.push_back(p->val);
                levelCount++;

                workStack.push(p);
            }
            while(!workStack.empty()){
                p = workStack.top();
                workStack.pop();
                if(level % 2 == 1){
                    if(p->right!=NULL)
                        workQueue.push(p->right);
                    if(p->left!=NULL)
                        workQueue.push(p->left);
                    
                }else{
                    if(p->left!=NULL)
                        workQueue.push(p->left);
                    if(p->right!=NULL)
                        workQueue.push(p->right);
                }
            }
            res.push_back(tempRes);
            tempRes.clear();
            level++;
            levelCount = 0;
        }
        return res;
    }
};

方法三、一个双端队列
根据不同的情形选择从对头如还是从队尾入

class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> res;
        if(root == nullptr)
            return res;
        vector<int> tempRes;
        deque<TreeNode*> workQueue;
        
        workQueue.push_back(root);
        bool leftRead = true;
        int levelNum = 0;
        int levelCount = 0;
        TreeNode* p;
        while(!workQueue.empty()){
            levelNum = workQueue.size();
            while(levelCount<levelNum){
                levelCount++;
                if(leftRead){
                    //从队头出
                    p = workQueue.front();
                    workQueue.pop_front();
                    tempRes.emplace_back(p->val);
                    //从队尾入
                    if(p->left != NULL)
                        workQueue.push_back(p->left);
                    if(p->right != NULL)
                        workQueue.push_back(p->right);
                }else{
                    //从队尾出
                    p = workQueue.back();
                    workQueue.pop_back();
                    tempRes.emplace_back(p->val);
                    //从队头入
                    if(p->right != NULL)
                        workQueue.push_front(p->right);
                    if(p->left != NULL)
                        workQueue.push_front(p->left);
                    
                }
            }
            levelCount = 0;
            res.emplace_back(tempRes);
            tempRes.clear();
            leftRead = !leftRead;
        }
        return res;
    }
};

你可能感兴趣的:(刷题笔记,队列,二叉树)