Use 102's result


  • 1
    M

    use the Binary Tree level order traversal result. and swap the result of odd row

    void zigzagLevelOrderImpl(TreeNode* root,int level)
    {
        if(!root)
            return;
        if(result.size()==level)
            result.push_back(vector<int>());
        result[level].push_back(root->val);
        
        zigzagLevelOrderImpl(root->left,level+1);
        zigzagLevelOrderImpl(root->right,level+1);
    }
    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
        zigzagLevelOrderImpl(root,0);
        for(int i=1;i<result.size();i+=2)
        {
            int size=result[i].size();
            for(int j=0;j<size/2;j++)
            {
                swap(result[i][j],result[i][size-j-1]);
            }
        }
        return result;
    }

Log in to reply
 

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