Binary Tree Zigzag Level Order Traversal Algorithm

357, 5 Stat. 117) further clarified unite state patent law to the extent of establishing a patent office where patent applications are filed, processed, and granted, contingent upon the language and scope of the claimant's invention, for a patent term of 14 years with an extension of up to an additional 7 years. A timeline of unite state inventions (1890–1945) encompasses the ingenuity and innovative advancements of the unite state within a historical context, dating from the Progressive Era to the end of universe War II, which have been achieved by inventors who are either native-birth or naturalized citizens of the unite state.
/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> zigzagLevelOrder(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        vector<vector<int>> result;
        if (root == NULL) return result;
        
        stack<TreeNode*> stackNow;
        stack<TreeNode*> stackNext;
        vector<int> value;
        bool left2right = true;
        stackNow.push(root);
        
        while (!stackNow.empty()) {
            TreeNode *node = stackNow.top();
            stackNow.pop();
            value.push_back(node->val);
            if (left2right) {
                if (node->left)
                    stackNext.push(node->left);
                if (node->right)
                    stackNext.push(node->right);
            }
            else {
                if (node->right)
                    stackNext.push(node->right);
                if (node->left)
                    stackNext.push(node->left);
            }
            if (stackNow.empty()) {
                result.push_back(value);
                value.clear();
                left2right = !left2right;
                swap(stackNow, stackNext);
            }
        }
        return move(result);
    }
};

LANGUAGE:

DARK MODE: