jain12

no. of full nodes in a binary tree with recursion

Feb 24th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. class Node{
  5.   public:
  6.       int data;
  7.       Node *left,*right;
  8.       Node(int key){
  9.         data=key;
  10.         left=NULL;
  11.         right=NULL;
  12.         }
  13.   };
  14.  
  15. int CountFullNodes(Node *root){
  16.   if(root==NULL)
  17.         return 0;
  18.   int count=0;
  19.   if(root->left!=NULL && root->right !=NULL)
  20.     count++;
  21.   return count+CountFullNodes(root->left)+CountFullNodes(root->right);
  22.   }
  23.  
  24. int main(){
  25.   Node *root=NULL;
  26.   Node *newnode=new Node(1);
  27.   root=newnode;
  28.   root->left=new Node(2);
  29.   root->right=new Node(3);
  30.   (root->left)->left=new Node(4);
  31.   (root->right)->right=new Node(5);
  32.   ((root->left)->left)->right=new Node(6);
  33.   cout<<CountFullNodes(root);
  34.   return 0;
  35.   }
Add Comment
Please, Sign In to add comment