Zigzag level order print of a tree

https://ide.codingblocks.com/s/241537 here’s my code for zigzag level order print which is giving tle error

@pooja_narula hey ,I have made changes to your code,please check them and fell free to ask any doubt:

you’ve not edited my code. you’ve just sent an altogether different code with different logic. please check my logic of the code once to find out what’s wrong

@pooja_narula hey the logic which you have written is not upto mark as it is not giving correct output,check this pseudo code:

void levelOrderZigZag(node *root) {
    stack<node *> s1;
    stack<node *> s2;
    bool leftOrRight = true;
    s1.push(root);
    while(!s1.empty() or !s2.empty()) {
        int row = 0;
        if(s1.size() > s2.size()) {
            row = s1.size();
        } else {
            row = s2.size();
        }
        while(row--) {
            if(leftOrRight) {
                node *curr = s1.top();
                s1.pop();
                cout<<curr->data<<" ";
                if(curr->left)
                    s2.push(curr->left);
                if(curr->right)
                    s2.push(curr->right);
            } else {
                node *curr = s2.top();
                s2.pop();
                cout<<curr->data<<" ";
                if(curr->right)
                    s1.push(curr->right);
                if(curr->left)
                    s1.push(curr->left);
            }
        }
        leftOrRight = !leftOrRight;
    }
}

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.