码迷,mamicode.com
首页 > 其他好文 > 详细

103. Binary Tree Zigzag Level Order Traversal

时间:2018-01-11 15:58:10      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:cto   row   http   turn   leetcode   body   find   val   nat   

欢迎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;
    }
};

题目来源

103. Binary Tree Zigzag Level Order Traversal

标签:cto   row   http   turn   leetcode   body   find   val   nat   

原文地址:https://www.cnblogs.com/ranjiewen/p/8267373.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!