[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;
        }
};

 

 

 
posted @ 2015-04-08 17:26  穆穆兔兔  阅读(225)  评论(0编辑  收藏  举报