C++ odd and even AC solution.


  • 0
    class Solution {
        vector<vector<int>> res;
        vector<int> emp;
    public:
        vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
            helper(root, 1);
            return res;
        }
        void helper(TreeNode* root, int level)
        {
            if(!root) return;
            if(level > res.size())
                res.push_back(emp);
            helper(root->left, level+1);
            if(level&1) 
                res[level-1].push_back(root->val);
            else
                res[level-1].insert(res[level-1].begin(),root->val);
            helper(root->right, level+1);
            
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.