Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<queue>
- using namespace std;
- class Node{
- public:
- int data;
- Node *left,*right;
- Node(int key){
- data=key;
- left=NULL;
- right=NULL;
- }
- };
- int CountHalfNodes(Node *root){
- if(root==NULL)
- return 0;
- if(root->left==NULL && root->right!=NULL)
- return 1+CountHalfNodes(root->right);
- if(root->right==NULL && root->left!=NULL)
- return 1+CountHalfNodes(root->left);
- return CountHalfNodes(root->left)+CountHalfNodes(root->right);
- }
- int main(){
- Node *root=NULL;
- Node *newnode=new Node(1);
- root=newnode;
- root->left=new Node(2);
- root->right=new Node(3);
- (root->left)->left=new Node(4);
- (root->right)->right=new Node(5);
- (root->right)->left=new Node(7);
- cout<<CountHalfNodes(root);
- return 0;
- }
Add Comment
Please, Sign In to add comment