LintCode-剑指Offer-(71)二叉树的锯齿形层次遍历

class Solution {
    /**
    * @param root: The root of binary tree.
    * @return: A list of lists of integer include
    *          the zigzag level order traversal of its nodes' values
    */
public:
    vector> zigzagLevelOrder(TreeNode *root) {
        // write your code here
        queue q;
        q.push(NULL);
        q.push(root);
        bool needreverse = false;
        vector> r;
        if (root==NULL)return r;
        int level = -1;
        while (q.size()!=1){
            if (q.front()==NULL){
                r.push_back(vector());
                level++;
                q.pop();
                q.push(NULL);
            }
            else{
                r[level].push_back(q.front()->val);
                if (q.front()->left!=NULL)q.push(q.front()->left);
                if (q.front()->right!=NULL)q.push(q.front()->right);
                q.pop();
            }
        }
        for (int i = 0;i

你可能感兴趣的:(LintCode)