Posts

Showing posts from July, 2021

bt take input

 binaryTree <int>* takeInput(){     int rootData;     cout<<"Enter data: ";     cin>>rootData;     if (rootData==-1){         return NULL;     }     binaryTree<int>* root=new binaryTree<int>(rootData);     binaryTree<int>* leftchild=takeInput();     binaryTree<int>* rightchild=takeInput();     root->left=leftchild;     root->right=rightchild;     return root; } //1 2 4 -1 -1 5 6 -1 -1 7 -1 -1 3 8 -1 -1 -1 

binary trees print

  #include<iostream> #include<vector> #include<queue> using namespace std; template <typename T> class binaryTree{     public:     T data;     binaryTree * left;     binaryTree * right;          binaryTree(T data){         this->data=data;         left=NULL;         right=NULL;     }     ~binaryTree(){         delete left;         delete right;     } }; void printTree(binaryTree<int>* root){     if(root==NULL){         return;     }     cout<<root->data<<":";     if(root->left!=NULL){         cout<<"L"<<root->left->data<<" ";              }     if(root->right!=NULL){       ...

Binary Tree class

  #include<iostream> #include<vector> #include<queue> using namespace std; template <typename T> class binaryTree{     public:     T data;     binaryTree * left;     binaryTree * right;          binaryTree(T data){         this->data=data;         left=NULL;         right=NULL;     }     ~binaryTree(){         delete left;         delete right;     } }; int main(){      }

Tree goodbye

 #include<iostream> #include<vector> #include<queue> using namespace std; template <typename T> class TreeNode {     public:     T data;     vector<TreeNode<T>*>children;          TreeNode(T data){         this->data=data;     } }; void printTree(TreeNode<int>* root){     cout<<root->data<<":";     for (int i=0;i<root->children.size();i++){         cout<<root->children[i]->data<<",";     }     cout<<endl;     for (int i=0;i<root->children.size();i++){         printTree(root->children[i]);     } } TreeNode<int>* takeInput(){     int rootData;     cout<<"Enter Data: ";     cin>>rootData;      TreeNode <int>* root = new TreeNode<int...

max sum of child of immediate node

 TreeNode<int>* maxSumNode(TreeNode<int>* root) {     // Write your code here     int sum=0;     //TreeNode<int>* max=new TreeNode<int>(-999);     for(int i=0;i<root->children.size();i++){         sum+=root->children[i]->data;     }     TreeNode<int>*max=new TreeNode<int>(sum);          for(int i=0;i<root->children.size();i++){                  TreeNode<int>* a=maxSumNode(root->children[i]);         if(a->data>max->data){             max=a;         }     }     return max;           }

Count nodes

 \ Given a tree and an integer x, find and return the number of nodes which contains data greater than x. Input format: The first line of input contains data of the nodes of the tree in level order form. The order is: data for root node, number of children to root node, data of each of child nodes and so on and so forth for each node. The data of the nodes of the tree is separated by space. The following line contains an integer, that denotes the value of x. Output Format : The first and only line of output prints the count of nodes greater than x. Constraints: Time Limit: 1 sec Sample Input 1 : 10 3 20 30 40 2 40 50 0 0 0 0 35 Sample Output 1 : 3 Sample Input 2 : 10 3 20 30 40 2 40 50 0 0 0 0 10 Sample Output 2: 5 int getLargeNodeCount(TreeNode<int>* root, int x) {     // Write your code here     int ans=0;     if (root==NULL){         return false;     }     if (root->data>x){     ...
Contains x Send Feedback Given a generic tree and an integer x, check if x is present in the given tree or not. Return true if x is present, return false otherwise. Input format : The first line of input contains data of the nodes of the tree in level order form. The order is: data for root node, number of children to root node, data of each of child nodes and so on and so forth for each node. The data of the nodes of the tree is separated by space. The following line contains an integer, that denotes the value of x. Output format : The first and only line of output contains true, if x is present and false, otherwise. Constraints: Time Limit: 1 sec Sample Input 1 : 10 3 20 30 40 2 40 50 0 0 0 0 40 Sample Output 1 : true Sample Input 2 : 10 3 20 30 40 2 40 50 0 0 0 0 4 Sample Output 2: false   bool isPresent(TreeNode<int>* root, int x) {     // Write your code here          if (root==NULL){         return false;  ...

Tree Destructor

 template <typename T>     class TreeNode {      public:         T data;         vector<TreeNode<T>*> children;              TreeNode(T data) {             this->data = data;         }              ~TreeNode() {             for (int i = 0; i < children.size(); i++) {                 delete children[i];             }         }     };

PostOrder Traversal

 void printPostOrder(TreeNode<int>* root) {     // Write your code here     if(root==NULL){         return;     }          for(int i=0;i<root->children.size();i++){         printPostOrder(root->children[i]);     }     cout<<root->data<<" "; }

PreOrder traversal

 void preorder(TreeNode<int>* root){     if(root==NULL){         return;     }     cout<<root->data<<" ";     for(int i=0;i<root->children.size();i++){         preorder(root->children[i]);     } }

Number of Leave Nodes

 int getLeafNodeCount(TreeNode<int>* root) {     // Write your code here     int ans=0;     if (root==NULL){         return 0;     }     if (root->children.size()==0){         ans=1;              }     for(int i=0;i<root->children.size();i++){         ans+=getLeafNodeCount(root->children[i]);     }     return ans; }

all elements at depth k

 void printAtLevel(TreeNode<int>* root, int k){     if (root==NULL){         return;     }     if (k==0){         cout<<root->data<<endl;         return;     }     for (int i=0;i<root->children.size();i++){         printAtLevel(root->children[i],k-1);     } }

Height of a tree

int getHeight(TreeNode<int>* root) {     // Write your code here     int ans=1;     int max=1;     for(int i=0;i<root->children.size();i++){         //max=0;                  ans+=getHeight(root->children[i]);         if (max<ans){             max=ans;         }         ans=1;              }     return max;          }

Max node of tree

 #include <climits> TreeNode<int>* maxDataNode(TreeNode<int>* root) {     // Write your code here     TreeNode<int>*max=new TreeNode<int>(root->data);          if (max->data<root->data){         max->data=root->data;         max=root;     }     for(int i=0;i<root->children.size();i++){         TreeNode<int>* a=maxDataNode(root->children[i]);         if (a->data>max->data){             max=a;         }              }     return max; }

Sum of Nodes of tree

Given a generic tree, find and return the sum of all nodes present in the given tree. Input format : The first line of input contains data of the nodes of the tree in level order form. The order is: data for root node, number of children to root node, data of each of child nodes and so on and so forth for each node. The data of the nodes of the tree is separated by space. Output Format : The first and only line of output prints the sum of all nodes of the given generic tree. Constraints: Time Limit: 1 sec Sample Input 1: 10 3 20 30 40 2 40 50 0 0 0 0 Sample Output 1: 190   int sumOfNodes(TreeNode<int>* root) {     // Write your code here     int ans=root->data;     for(int i=0;i<root->children.size();i++){         ans+=sumOfNodes(root->children[i]);     }     return ans;      }

number of nodes of trees

 int numNodes(TreeNode<int>* root){     int ans=1;     for(int i=0;i<root->children.size();i++){         ans+=numNodes(root->children[i]);     }     return ans; }

Level Wise Print

 /************************************************************       Following is the structure for the TreeNode class     template <typename T>     class TreeNode {      public:         T data;         vector<TreeNode<T>*> children;              TreeNode(T data) {             this->data = data;         }              ~TreeNode() {             for (int i = 0; i < children.size(); i++) {                 delete children[i];             }         }     }; ************************************************************/ void printLevelWise(TreeNode<int>* root) {     // Write your code here       ...

Take Input Level Wise

 TreeNode<int>* takeInputLevel(){     int rootData;     cout<<"Enter Data: "<<endl;     cin>>rootData;     TreeNode <int>* root = new TreeNode<int>(rootData);     queue<TreeNode<int>*> pendingNode;//queue     pendingNode.push(root);     while(pendingNode.size()!=0){         TreeNode<int>*front = pendingNode.front();         pendingNode.pop();         cout<<"Enter the number of children of "<<front->data<<endl;         int childnum;         cin>>childnum;         for (int i=0;i<childnum;i++){             cout<<"Enter the "<<i<<"th data of root"<<front->data<<endl;             int childata;         ...

Print tree rev, take input

#include<iostream> #include<vector> using namespace std; template <typename T> class TreeNode {     public:     T data;     vector<TreeNode<T>*>children;          TreeNode(T data){         this->data=data;     } }; void printTree(TreeNode<int>* root){     cout<<root->data<<":";     for (int i=0;i<root->children.size();i++){         cout<<root->children[i]->data<<",";     }     cout<<endl;     for (int i=0;i<root->children.size();i++){         printTree(root->children[i]);     } } TreeNode<int>* takeInput(){     int rootData;     cout<<"Enter Data: ";     cin>>rootData;      TreeNode <int>* root = new TreeNode<int>(rootData);     c...

Tree_Take_input

  TreeNode <int>* takeInput(){     int rootData;     cout<<"Enter data: ";     cin>>rootData;     TreeNode <int>* root = new TreeNode<int>(rootData);     int n;     cout<<"Enter number of children of "<<rootData<<": ";     cin>>n;     for(int i=0;i<n;i++){         TreeNode<int>* child=takeInput();         root->children.push_back(child);     }     return root;      }

tree_use_case.cpp

 // Online C++ compiler to run C++ program online #include <iostream> #include <vector> using namespace std; template <typename T> class TreeNode {     public:     T data;     vector<TreeNode<T>*> children;          TreeNode(T data){         this->data=data;     } }; void printTree(TreeNode<int>* root){          if (root==NULL){         return;     }     cout<<root->data<<":";     for (int i=0;i<root->children.size();i++){         cout<<root->children[i]->data<<",";     }     cout<<endl;     for (int i=0;i<root->children.size();i++){         printTree(root->children[i]);     } } int main(){     TreeNode <int>* root = new TreeNode<int...

TreeNode.h

 // Online C++ compiler to run C++ program online #include <iostream> #include <vector> template <typename T> class TreeNode {     public:     T data;     vector <TreeNode<T>*> children;          TreeNode(T data){         this->data=data;     } }

Vectors class 1

  // Online C++ compiler to run C++ program online #include <iostream> #include <vector> using namespace std; int main(){     vector<int> v;          for (int i=0;i<100;i++){         cout<<"cap: "<<v.capacity()<<endl;         cout<<"size: "<<v.size()<<endl;         v.push_back(i);                       }     //v.popback()     //v[1]=100;     //cout<<v.at(2) }

Merge two sorted Linked Lists

Code: Merge Two Sorted LL Send Feedback You have been given two sorted(in ascending order) singly linked lists of integers. Write a function to merge them in such a way that the resulting singly linked list is also sorted(in ascending order) and return the new head to the list. Note : Try solving this in O(1) auxiliary space. No need to print the list, it has already been taken care. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case or query contains the elements of the first sorted singly linked list separated by a single space. The second line of the input contains the elements of the second sorted singly linked list separated by a single space. Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element Output : For each test case/query, print the result...

Reverse Print

 void rev_display(node*head){     if(head==NULL){         cout<<endl;         return;     }          rev_display(head->next);     cout<<head->data<<" "; }

Mid Point of Linked List

  Code: Midpoint of LL Send Feedback For a given singly linked list of integers, find and return the node present at the middle of the list. Note : If the length of the singly linked list is even, then return the first middle node. Example: Consider, 10 -> 20 -> 30 -> 40 is the given list, then the nodes present at the middle with respective data values are, 20 and 30. We return the first node with data 20.  Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first and the only line of each test case or query contains the elements of the singly linked list separated by a single space. Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element  Output Format : For each test case/query, print the data value of the node at the middle of the given list. Output for every test c...

Check palindrome LL

Palindrome LinkedList Send Feedback You have been given a head to a singly linked list of integers. Write a function check to whether the list given is a 'Palindrome' or not.  Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. First and the only line of each test case or query contains the the elements of the singly linked list separated by a single space.  Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element.  Output format : For each test case, the only line of output that print 'true' if the list is Palindrome or 'false' otherwise.  Constraints : 1 <= t <= 10^2 0 <= M <= 10^5 Time Limit: 1sec Where 'M' is the size of the singly linked list. Sample Input 1 : 1 9 2 3 3 2 9 -1 Sample Output 1 : true Sample Input 2 : 2 0 2 3 2 5 -1 -1 Sample Ou...

Reverse A LL

Print Reverse LinkedList Send Feedback You have been given a singly linked list of integers. Write a function to print the list in a reverse order. To explain it further, you need to start printing the data from the tail and move towards the head of the list, printing the head data at the end. Note : You can’t change any of the pointers in the linked list, just print it in the reverse order.  Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first and the only line of each test case or query contains the elements of the singly linked list separated by a single space. Remember/Constraints : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element. Output format : For each test case, print the singly linked list of integers in a reverse fashion, in a row, separated by a single space. Output for every test c...

Remove Duplicate LL

  Eliminate duplicates from LL Send Feedback You have been given a singly linked list of integers where the elements are sorted in ascending order. Write a function that removes the consecutive duplicate values such that the given list only contains unique elements and returns the head to the updated list.  Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first and the only line of each test case or query contains the elements(in ascending order) of the singly linked list separated by a single space.  Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element.  Output format : For each test case/query, print the resulting singly linked list of integers in a row, separated by a single space. Output for every test case will be printed in a seperate line. Constraints : 1 <= t...

Shift Last N elements to start

  AppendLastNToFirst Send Feedback You have been given a singly linked list of integers along with an integer 'N'. Write a function to append the last 'N' nodes towards the front of the singly linked list and returns the new head to the list. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case or query contains the elements of the singly linked list separated by a single space. The second line contains the integer value 'N'. It denotes the number of nodes to be moved from last to the front of the singly linked list. Remember/Consider : While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element. Output format : For each test case/query, print the resulting singly linked list of integers in a row, separated by a single space. Output for every test case will...