ann8497

spiral

Aug 7th, 2019
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. void printSpiral(Node *root)
  2. {
  3.    if(root == NULL)return;
  4.    deque<Node*>q;
  5.    q.push_front(root);
  6.    int LtoR = 0;
  7.    while(!q.empty()){
  8.       int sz = q.size();
  9.       while(sz--){
  10.           Node*temp;
  11.           if(LtoR){
  12.               temp = q.front();
  13.               cout<<temp->data<<" ";
  14.               q.pop_front();
  15.               if(temp->left)q.push_back(temp->left);
  16.               if(temp->right)q.push_back(temp->right);
  17.           }
  18.           else{
  19.               temp = q.back();
  20.               cout<<temp->data<<" ";
  21.               q.pop_back();
  22.              
  23.               if(temp->right)q.push_front(temp->right);
  24.               if(temp->left)q.push_front(temp->left);
  25.           }
  26.       }
  27.       LtoR = 1 - LtoR;
  28.    }
  29. }
Add Comment
Please, Sign In to add comment