Advertisement
imashutosh51

ZigZag Tree Traversa

Jul 31st, 2022 (edited)
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. //Method 1: Time complexity O(n) but two iteration
  2. /*Logic:
  3. we will travere the tree levelwise and we will switch the flag levelwise,while traversing  nodes of a level,we
  4. wiil push those nodes in a temporaray vector and then print those vectors in alternate direction,one time normal
  5. and next ime reversed
  6. */
  7. class Solution{
  8.     public:
  9.     //Function to store the zig zag order traversal of tree in a list.
  10.     vector <int> zigZagTraversal(Node* root)
  11.     {
  12.         if(root == NULL){return {  } ; }
  13.      
  14.         vector<int > ans ;
  15.         queue<Node*> q ;
  16.         q.push(root) ;
  17.         bool flag = false ;
  18.      
  19.         while(!q.empty()){
  20.             int size = q.size() ;
  21.             vector<int> level ;
  22.             for(int i=0 ; i < size ; i++){
  23.                 Node* node = q.front() ;
  24.                 q.pop() ;
  25.                 level.push_back(node->data) ;
  26.      
  27.                 if(node->left != NULL) {q.push(node->left) ;}
  28.                 if(node->right != NULL) {q.push(node->right) ;}  
  29.      
  30.             }
  31.             flag = !flag ;  
  32.             if(flag == false){
  33.                 reverse(level.begin() , level.end()) ;          
  34.             }
  35.             for(int i = 0 ; i < level.size() ; i++){
  36.               ans.push_back(level[i]) ;
  37.             }
  38.              
  39.         }
  40.         return ans ;
  41.     }
  42. };
  43. //Method 2:- Single pass O(n)
  44. /*
  45. Logic:
  46. We will use deque for storing the nodes of tree and we will keep a flag which will alternate it's
  47. value at every level.Once you push the left and right node at back and pop from front.and second time
  48. you push right and left from from and pop from back.
  49. */
  50. class Solution{
  51.     public:
  52.     vector <int> zigZagTraversal(Node* root)
  53.     {
  54.         vector <int> ans;
  55.         deque<Node*> q;
  56.         q.push_back(root);
  57.         bool flag=true;
  58.         while(!q.empty()){
  59.             int k=q.size();
  60.             for(int i=0;i<k;i++){
  61.                 Node *temp;
  62.                 if(flag){
  63.                     temp=q.front();
  64.                     q.pop_front();
  65.                     ans.push_back(temp->data);
  66.                     if(temp->left) q.push_back(temp->left);
  67.                     if(temp->right) q.push_back(temp->right);
  68.                    
  69.                 }
  70.                 else{
  71.                     temp=q.back();
  72.                     q.pop_back();
  73.                     ans.push_back(temp->data);
  74.                     if(temp->right) q.push_front(temp->right);
  75.                     if(temp->left) q.push_front(temp->left);
  76.                 }
  77.             }
  78.             flag=!flag;
  79.         }
  80.         return ans;
  81.     }
  82. };
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement