103. Binary Tree Zigzag Level Order Traversal

欢迎fork and star:Nowcoder-Repository-github

103. 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.

OJ's Binary Tree Serialization:

The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

Here's an example:

   1
  / \
 2   3
    /
   4
    \
     5

The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}". 

解析

  • 思路1:对奇数行vector进行反转
// 103. Binary Tree Zigzag Level Order Traversal
class Solution_103 {
public:
    vector<vector<int> > zigzagLevelOrder(TreeNode *root) {

        vector<vector<int> > vecs;
        if (!root)
        {
            return vecs;
        }

        queue<TreeNode*> que;
        que.push(root);

        int level = 0;
        while (!que.empty())
        {
            int size = que.size();
            vector<int> vec;
            while (size--)
            {
                TreeNode* temp = que.front();
                que.pop();
                vec.push_back(temp->val);
                if (temp->left)
                {
                    que.push(temp->left);
                }
                if (temp->right)
                {
                    que.push(temp->right);
                }
            }
            level++;
            if (!(level % 2))  //实际是偶数行反转,从第0行开始       //奇数层反转vector  bug:level % 2 != 0 
            {
                reverse(vec.begin(), vec.end());
            }
            vecs.push_back(vec);
        }

        return vecs;
    }
};
  • 思路2:设置标志位,对奇数行取vector对应的元素
vector<vector<int> > zigzagLevelOrder(TreeNode* root) {
    if (root == NULL) {
        return vector<vector<int> > ();
    }
    vector<vector<int> > result;

    queue<TreeNode*> nodesQueue;
    nodesQueue.push(root);
    bool leftToRight = true;

    while ( !nodesQueue.empty()) {
        int size = nodesQueue.size();
        vector<int> row(size);
        for (int i = 0; i < size; i++) {
            TreeNode* node = nodesQueue.front();
            nodesQueue.pop();

            // find position to fill node's value
            int index = (leftToRight) ? i : (size - 1 - i);

            row[index] = node->val;
            if (node->left) {
                nodesQueue.push(node->left);
            }
            if (node->right) {
                nodesQueue.push(node->right);
            }
        }
        // after this level
        leftToRight = !leftToRight;
        result.push_back(row);
    }
    return result;
}
  • 思路3:使用deque双端队列,注意取back()元素,先进right孩子

class Solution {
public:
    vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
        vector<vector<int> > ans;
        if(root == NULL) return ans;
        deque<TreeNode*> dq; 
        dq.push_back(root);
        int zz = 0;
        while(dq.size())
        {
            vector<int> tmp;
            int h = dq.size();
            int l = 0;
            TreeNode *t;
            while(l < h)
            {
                if(zz % 2 == 0)
                {
                    t = dq.front();
                    dq.pop_front();
                    if(t->left)  dq.push_back(t->left);
                    if(t->right) dq.push_back(t->right);
                    tmp.push_back(t->val);
                    h--;
                }
                else
                {
                    t = dq.back();
                    dq.pop_back();
                    if(t->right) dq.push_front(t->right);
                    if(t->left) dq.push_front(t->left);
                    tmp.push_back(t->val);
                    h--;
                }
            }
            ans.push_back(tmp);
            zz ++;
        }
        return ans;
    }
};

题目来源

    原文作者:ranjiewen
    原文地址: https://www.cnblogs.com/ranjiewen/p/8267373.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞