jayati

Level order traversal in spiral form

Oct 12th, 2023
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.97 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. // Tree Node
  6. struct Node
  7. {
  8.     int data;
  9.     Node* left;
  10.     Node* right;
  11. };
  12. // Utility function to create a new Tree Node
  13. Node* newNode(int val)
  14. {
  15.     Node* temp = new Node;
  16.     temp->data = val;
  17.     temp->left = NULL;
  18.     temp->right = NULL;
  19.  
  20.     return temp;
  21. }
  22.  
  23. vector<int> findSpiral(Node *root);
  24.  
  25. // Function to Build Tree
  26. Node* buildTree(string str)
  27. {
  28.     // Corner Case
  29.     if(str.length() == 0 || str[0] == 'N')
  30.         return NULL;
  31.  
  32.     // Creating vector of strings from input
  33.     // string after spliting by space
  34.     vector<string> ip;
  35.  
  36.     istringstream iss(str);
  37.     for(string str; iss >> str; )
  38.         ip.push_back(str);
  39.  
  40.     // Create the root of the tree
  41.     Node* root = newNode(stoi(ip[0]));
  42.  
  43.     // Push the root to the queue
  44.     queue<Node*> queue;
  45.     queue.push(root);
  46.  
  47.     // Starting from the second element
  48.     int i = 1;
  49.     while(!queue.empty() && i < ip.size()) {
  50.  
  51.         // Get and remove the front of the queue
  52.         Node* currNode = queue.front();
  53.         queue.pop();
  54.  
  55.         // Get the current node's value from the string
  56.         string currVal = ip[i];
  57.  
  58.         // If the left child is not null
  59.         if(currVal != "N") {
  60.  
  61.             // Create the left child for the current node
  62.             currNode->left = newNode(stoi(currVal));
  63.  
  64.             // Push it to the queue
  65.             queue.push(currNode->left);
  66.         }
  67.  
  68.         // For the right child
  69.         i++;
  70.         if(i >= ip.size())
  71.             break;
  72.         currVal = ip[i];
  73.  
  74.         // If the right child is not null
  75.         if(currVal != "N") {
  76.  
  77.             // Create the right child for the current node
  78.             currNode->right = newNode(stoi(currVal));
  79.  
  80.             // Push it to the queue
  81.             queue.push(currNode->right);
  82.         }
  83.         i++;
  84.     }
  85.  
  86.     return root;
  87. }
  88.  
  89.  
  90. int main() {
  91.     int t;
  92.     string  tc;
  93.     getline(cin,tc);
  94.     t=stoi(tc);
  95.     while(t--)
  96.     {
  97.         string s;
  98.         getline(cin,s);
  99.         Node* root = buildTree(s);
  100.  
  101.         vector<int> vec = findSpiral(root);
  102.         for(int x : vec)
  103.         cout<<x<<" ";
  104.         cout << endl;
  105.     }
  106.     return 0;
  107. }
  108.  
  109.  
  110.  
  111. // } Driver Code Ends
  112.  
  113.  
  114. /* A binary tree node has data, pointer to left child
  115.    and a pointer to right child  
  116. struct Node
  117. {
  118.     int data;
  119.     struct Node* left;
  120.     struct Node* right;
  121.    
  122.     Node(int x){
  123.         data = x;
  124.         left = right = NULL;
  125.     }
  126. }; */
  127.  
  128.  
  129. //Function to return a list containing the level order traversal in spiral form.
  130. int l=0;
  131. int height(Node* node)
  132. {
  133.     if (node == NULL)
  134.         return 0;
  135.     else {
  136.          
  137.         // Compute the height of each subtree
  138.         int lheight = height(node->left);
  139.         int rheight = height(node->right);
  140.  
  141.         // Use the larger one
  142.         if (lheight > rheight) {
  143.             return (lheight + 1);
  144.         }
  145.         else {
  146.             return (rheight + 1);
  147.         }
  148.     }
  149. }
  150. // Print nodes at a given level
  151. void printGivenLevel(Node* root, int level, int ltr,vector<int> &ans)
  152. {
  153.     if (root == NULL)
  154.         return;
  155.     if (level == 1)
  156.         ans.push_back(root->data);
  157.  
  158.     else if (level > 1) {
  159.         if (ltr) {
  160.             printGivenLevel(root->left, level - 1, ltr,ans);
  161.             printGivenLevel(root->right, level - 1, ltr,ans);
  162.         }
  163.         else {
  164.             printGivenLevel(root->right, level - 1, ltr,ans);
  165.             printGivenLevel(root->left, level - 1, ltr,ans);
  166.         }
  167.     }
  168. }
  169. void printSpiral(Node* root,vector<int> &ans)
  170. {
  171.     int h = height(root);
  172.     int i;
  173.  
  174.  
  175.     bool ltr = false;
  176.     for (i = 1; i <= h; i++) {
  177.         printGivenLevel(root, i, ltr,ans);
  178.         ltr = !ltr;
  179.     }
  180. }
  181.  
  182.  
  183.  
  184.  
  185.  
  186. vector<int> findSpiral(Node *root)
  187. {
  188.     //Your code here
  189.     vector<int> ans;
  190.     if(root==NULL)
  191.     {
  192.         return ans;
  193.     }
  194.    
  195.     printSpiral(root,ans);
  196.     return ans;
  197. }
  198.  
Advertisement
Add Comment
Please, Sign In to add comment