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