class Solution {
public:    
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        vector<vector<int>> V;
        if (root != NULL)
        {
            int i = 0;
            queue<TreeNode*> Q;
            Q.push(root);
            while (!Q.empty())
            {
                vector<TreeNode*> T;
                vector<int> v;
                while (!Q.empty())
                {
                    TreeNode* node = Q.front();
                    Q.pop();

                    T.push_back(node);
                    v.push_back(node->val);
                }
                if (i % 2 == 1)
                {                    
                    vector<int> vv;
                    for (int j = v.size() - 1; j >= 0; j--)
                    {
                        vv.push_back(v[j]);
                    }
                    V.push_back(vv);
                }
                else
                {
                    V.push_back(v);
                }
                for (auto t : T)
                {
                    if (t->left != NULL)
                    {
                        Q.push(t->left);
                    }
                    if (t->right != NULL)
                    {
                        Q.push(t->right);
                    }
                }
                i++;
            }
        }
        return V;
    }
};

 

posted on 2018-10-05 16:51  Sempron2800+  阅读(132)  评论(0编辑  收藏  举报