6ms Simple C++ solution


  • 1
    X
    class Solution {
    private:
        vector<vector<int> >resultList;
        
        void zigzagOrderInner(TreeNode *root, int level) {
            if(!root) return;
            if(level == resultList.size())
                resultList.push_back(vector<int>());
            if(level%2 == 0)
                resultList[level].push_back(root->val);  
            else {
                resultList[level].insert(resultList[level].begin(), root->val);
            }
            if(root->left) zigzagOrderInner(root->left, level+1);
            if(root->right) zigzagOrderInner(root->right, level+1);
        }
        
    public:
        vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
            zigzagOrderInner(root, 0);
            return resultList;
        }
    };

  • 0
    R

    I like this recursive Solution


Log in to reply
 

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