[sword finger offer] print binary tree (binary tree) in zigzag order

Title Description

Please implement a function to print the binary tree in zigzag, that is, the first line is printed from left to right, the second layer is printed from right to left, the third line is printed from left to right, and so on.

thinking

[1] similar to the previous topic, the difference is that a variable is used to control the direction of printing. When each layer is traversed, the queue storage is used to traverse the layer. Only when the last printing is used to determine whether it needs to be flipped.
Disadvantage: the current sequence needs to be flipped when even layers are printed

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
    vector<vector<int> > Print(TreeNode* pRoot) {
        vector<vector<int> > ret;
        vector<int> tmp;

        if(pRoot == nullptr)
            return ret;

        int toBePrinted = 1;    // Number of nodes not printed in current layer
        int nextLevel = 0;      // Number of nodes to be printed in the next layer
        bool flag = true;    // Next layer printing direction
        queue<TreeNode *> q;
        q.push(pRoot);
        while (!q.empty()){
            TreeNode *node = q.front();
            if (node->left != nullptr){
                nextLevel++;
                q.push(node->left);
            }

            if(node->right != nullptr){
                nextLevel++;
                q.push(node->right);
            }

            tmp.push_back(node->val);
            q.pop();
            toBePrinted --;
            if (toBePrinted == 0){
                if(!flag)       // If the current layer needs to be printed from right to left
                    reverse(tmp.begin(),tmp.end());
                ret.push_back(tmp);
                tmp.clear();    // Reset current layer print record
                flag = !flag;   // Print direction flip
                toBePrinted = nextLevel;
                nextLevel = 0;
            }
        }
        return ret;
    }
};

[2] use two stacks to alternately store nodes of different layers

Output without flipping

class Solution {
public:
    vector<vector<int> > Print(TreeNode *pRoot) {
        vector<vector<int> > ret;

        if(pRoot == nullptr)
            return ret;

        vector<int> tmp;
        stack<TreeNode*> s[2];
        int current = 0;    // Current stack number
        int next =1;        // Next stack number
        s[current].push(pRoot);

        while (!s[0].empty() || !s[1].empty()){
            TreeNode *node = s[current].top();

            if(current == 0){
                if(node->left!= nullptr)
                    s[next].push(node->left);
                if(node->right!= nullptr)
                    s[next].push(node->right);
            } else{
                if(node->right!= nullptr)
                    s[next].push(node->right);
                if(node->left!= nullptr)
                    s[next].push(node->left);
            }

            tmp.push_back(node->val);
            s[current].pop();
            if(s[current].empty()){
                current = 1-current;    // Current stack empty to next level
                next = 1-next;
                ret.push_back(tmp);
                tmp.clear();
            }
        }

        return  ret;
    }
};```


Posted by eheia on Thu, 28 Nov 2019 09:44:06 -0800