jain12

no. of half nodes without using recursion.

Feb 24th, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.37 KB | None | 0 0
  1. #include<iostream>
  2. #include<queue>
  3. using namespace std;
  4.  
  5. class Node{
  6.   public:
  7.       int data;
  8.       Node *left,*right;
  9.       Node(int key){
  10.         data=key;
  11.         left=NULL;
  12.         right=NULL;
  13.         }
  14.   };
  15.  
  16. int CountLeaves(Node *root){
  17.   if(root==NULL)
  18.       return 0;
  19.   int count=0;
  20.   queue<Node *>q;
  21.   q.push(root);
  22.   while(!q.empty()){
  23.     Node *temp=q.front();
  24.     q.pop();
  25.     if(temp->left==NULL && temp->right==NULL)
  26.         count++;
  27.     if(temp->left!=NULL)
  28.         q.push(temp->left);
  29.     if(temp->right!=NULL)
  30.         q.push(temp->right);
  31.     }
  32.     return count;
  33.   }
  34.  
  35. int CountFullNodes(Node *root){
  36.   if(root==NULL)
  37.       return 0;
  38.   int count=0;
  39.   queue<Node *>q;
  40.   q.push(root);
  41.   while(!q.empty()){
  42.     Node *temp=q.front();
  43.     q.pop();
  44.     if(temp->left!=NULL && temp->right!=NULL)
  45.         count++;
  46.     if(temp->left!=NULL)
  47.         q.push(temp->left);
  48.     if(temp->right!=NULL)
  49.         q.push(temp->right);
  50.     }
  51.     return count;
  52.   }
  53.  
  54. int CountNodes(Node *root){
  55.   if(root==NULL)
  56.       return 0;
  57.   int count=0;
  58.   queue<Node *>q;
  59.   q.push(root);
  60.   while(!q.empty()){
  61.     Node *temp=q.front();
  62.     q.pop();
  63.         count++;
  64.     if(temp->left!=NULL)
  65.         q.push(temp->left);
  66.     if(temp->right!=NULL)
  67.         q.push(temp->right);
  68.     }
  69.     return count;
  70.   }
  71.  
  72. int CountHalfNodes(Node *root){    //by using previous functions(CountLeaves,CountFull)
  73.   if(root==NULL)
  74.         return 0;
  75.   int total= CountNodes(root);
  76.   int full= CountFullNodes(root);
  77.   int leaves= CountLeaves(root);
  78.   return total-full-leaves;
  79.   }
  80.  
  81. int countHalfNodes(Node *root){    //without using previous functions
  82.    if(root==NULL)
  83.       return 0;
  84.   int count=0;
  85.   queue<Node *>q;
  86.   q.push(root);
  87.   while(!q.empty()){
  88.     Node *temp=q.front();
  89.     q.pop();
  90.     if((temp->left==NULL &&temp->right!=NULL) ||(temp->right ==NULL && temp->left !=NULL))
  91.         count++;
  92.     if(temp->left!=NULL)
  93.         q.push(temp->left);
  94.     if(temp->right!=NULL)
  95.         q.push(temp->right);
  96.     }
  97.     return count;
  98.   }
  99.  
  100. int main(){
  101.   Node *root=NULL;
  102.   Node *newnode=new Node(1);
  103.   root=newnode;
  104.   root->left=new Node(2);
  105.   root->right=new Node(3);
  106.   (root->left)->left=new Node(4);
  107.   (root->right)->right=new Node(5);
  108.   (root->right)->left=new Node(7);
  109.   cout<<CountHalfNodes(root);
  110.   cout<<endl<<countHalfNodes(root);
  111.   return 0;
  112.   }
Add Comment
Please, Sign In to add comment