jayati

Diameter of a binary tree

Oct 12th, 2023
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. /* A binary tree node has data, pointer to left child
  6.    and a pointer to right child */
  7. struct Node {
  8.     int data;
  9.     struct Node* left;
  10.     struct Node* right;
  11. };
  12. Node* newNode(int val) {
  13.     Node* temp = new Node;
  14.     temp->data = val;
  15.     temp->left = NULL;
  16.     temp->right = NULL;
  17.     return temp;
  18. }
  19. Node* buildTree(string str) {
  20.     // Corner Case
  21.     if (str.length() == 0 || str[0] == 'N') return NULL;
  22.  
  23.     // Creating vector of strings from input
  24.     // string after spliting by space
  25.     vector<string> ip;
  26.  
  27.     istringstream iss(str);
  28.     for (string str; iss >> str;) ip.push_back(str);
  29.  
  30.     // Create the root of the tree
  31.     Node* root = newNode(stoi(ip[0]));
  32.  
  33.     // Push the root to the queue
  34.     queue<Node*> queue;
  35.     queue.push(root);
  36.  
  37.     // Starting from the second element
  38.     int i = 1;
  39.     while (!queue.empty() && i < ip.size()) {
  40.  
  41.         // Get and remove the front of the queue
  42.         Node* currNode = queue.front();
  43.         queue.pop();
  44.  
  45.         // Get the current node's value from the string
  46.         string currVal = ip[i];
  47.  
  48.         // If the left child is not null
  49.         if (currVal != "N") {
  50.  
  51.             // Create the left child for the current node
  52.             currNode->left = newNode(stoi(currVal));
  53.  
  54.             // Push it to the queue
  55.             queue.push(currNode->left);
  56.         }
  57.  
  58.         // For the right child
  59.         i++;
  60.         if (i >= ip.size()) break;
  61.         currVal = ip[i];
  62.  
  63.         // If the right child is not null
  64.         if (currVal != "N") {
  65.  
  66.             // Create the right child for the current node
  67.             currNode->right = newNode(stoi(currVal));
  68.  
  69.             // Push it to the queue
  70.             queue.push(currNode->right);
  71.         }
  72.         i++;
  73.     }
  74.  
  75.     return root;
  76. }
  77.  
  78.  
  79. // } Driver Code Ends
  80. /* Tree node structure  used in the program
  81.  
  82. struct Node
  83. {
  84.     int data;
  85.     struct Node* left;
  86.     struct Node* right;
  87.  
  88.     Node(int x){
  89.         data = x;
  90.         left = right = NULL;
  91.     }
  92. }; */
  93.  
  94. class Solution {
  95.   public:
  96.     // Function to return the diameter of a Binary Tree.
  97.     int solve(Node* root,int &ans)
  98.     {
  99.         if(root==NULL)
  100.         {
  101.             return 0;
  102.         }
  103.         int l=solve(root->left,ans);
  104.         int r=solve(root->right,ans);
  105.         ans = max(ans,l+r+1);
  106.        
  107.         return 1+max(l,r);
  108.     }
  109.     int diameter(Node* root) {
  110.         // Your code here
  111.         int ans=INT_MIN;
  112.         solve(root,ans);
  113.         return ans;
  114.     }
  115. };
  116.  
  117. //{ Driver Code Starts.
  118.  
  119. /* Driver program to test size function*/
  120. int main() {
  121.     int t;
  122.     scanf("%d\n", &t);
  123.     while (t--) {
  124.         string s;
  125.         getline(cin, s);
  126.         Node* root = buildTree(s);
  127.         Solution ob;
  128.         cout << ob.diameter(root) << endl;
  129.     }
  130.     return 0;
  131. }
  132.  
  133. // } Driver Code Ends
Advertisement
Add Comment
Please, Sign In to add comment