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