• [LeetCode] 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,#,#,15,7},

        3
       / 
      9  20
        /  
       15   7
    

    return its zigzag level order traversal as:

    [
      [3],
      [20,9],
      [15,7]
    ]
    

    confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

    Hide Tags
     Tree Breadth-first Search Stack
     
     
    方法一:Binary Tree Level Order Traversal 的双queue法略加修改
    /**
     * Definition for binary tree
     * 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)
            {
                queue<TreeNode*> q1;
                queue<TreeNode*> q2; 
                vector<vector<int> > res;
    
                int depth = 0;
                bool needReverse = false;
                if(root != NULL)
                {   
                    q1.push(root);
                    res.push_back(vector<int> ());
                }   
        
                while(!q1.empty())
                {   
                    TreeNode * p = q1.front();
                    q1.pop();
    
                    res[depth].push_back(p->val);
    
                    if(p->left)
                        q2.push(p->left);
                    if(p->right)
                        q2.push(p->right);
                    if(q1.empty())
                    {   
                        if(needReverse)
                            reverse(res[depth].begin(), res[depth].end());
                        needReverse = ! needReverse;
                        if(!q2.empty())
                        {   
                            swap(q1, q2);
                            depth ++;
                            res.push_back(vector<int> ());
                        }
                    }
                }
                return res;
            }
    };
     
    方法二:一个queue,用NULL来表示一层的结束,在取出的是NULL后,处理这一层,同时如果queue中还有元素,就在压入一个NULL,方法很巧妙
     
    class Solution {
        public:
            vector<vector<int> > zigzagLevelOrder(TreeNode *root)
            {
                vector<vector<int> > res;
                if(root == NULL)
                    return res;
    
                queue<TreeNode*> q;
                vector<int> curVec;
    
                q.push(root);
                q.push(NULL); // mark the layer's end
                bool isLeft2Right = true;
    
                while(!q.empty())
                {   
                    TreeNode * p = q.front();
                    q.pop();
    
                    if(p == NULL)
                    {   
                        if(!isLeft2Right)
                        {   
                            reverse(curVec.begin(), curVec.end());
                        }   
                        res.push_back(curVec);
                        isLeft2Right = !isLeft2Right;
                        curVec.clear();
                        if(!q.empty())// if elements exist in queue
                            q.push(NULL);// mark the layer's end
        
                    }   
                    else
                    {
                        curVec.push_back(p->val);
    
                        if(p->left)
                            q.push(p->left);
                        if(p->right)
                            q.push(p->right);
                    }
                }
                return res;
            }
    };
     

     
  • 相关阅读:
    Js全选 添加和单独删除
    H5新手快速入门 简单布局
    DOM 节点 课程表
    Datalogic组网模式下通讯
    svn检出的时候报 Unable to connect to a repository at URL错误(摘自CSDN)
    Subsonic使用中
    HTTP 错误 500.21
    WinForm下增加声音提示
    IIS7.0发布Web服务器0002
    IIS7.0发布Web服务-0001
  • 原文地址:https://www.cnblogs.com/diegodu/p/4403003.html
Copyright © 2020-2023  润新知