please convert to C languange   #include using namespace std; class tree{ //tree node     public:         int data;         tree *left;         tree *right; };

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

please convert to C languange

 

#include<bits/stdc++.h>
using namespace std;

class tree{ //tree node
    public:
        int data;
        tree *left;
        tree *right;
};

bool hasRootToLeafSum(tree *root, int s){
    bool path=false; //declare boolean variable path
    //base condition checking
    if(root==NULL && s==0)
        return true;

    s-=root->data; //subtract current root value

    //checking whether leaf node reached and remaining sum =0
    if(s==0 && root->left==NULL && root->right==NULL) 
        return true;
    //recursively done for both subtrees
    if(root->left){//for left subtree
        path=path||hasRootToLeafSum(root->left, s);
    }
    if(root->right){//for right subtree
        path=path||hasRootToLeafSum(root->right, s);
    }
    return path;
}


tree* newnode(int data){  //creating new nodes
    tree* node = (tree*)malloc(sizeof(tree)); 
    node->data = data; 
    node->left = NULL; 
    node->right = NULL; 
    return(node); 

int main() { 
    //**same tree is builted as shown in example**
    cout<<"tree in the example is build here"<<endl;
    //building the tree like as in the example
    tree *root=newnode(8); 
    root->left= newnode(5); 
    root->right= newnode(4); 
    root->right->right=newnode(11);
    root->right->right->left=newnode(3);
    root->left->left=newnode(9); 
    root->left->right=newnode(7);
    root->left->right->left=newnode(1);
    root->left->right->right=newnode(12);
    root->left->right->right->left=newnode(2);

    int s;

    cout<<"enter input sum S......"<<endl;
    cin>>s;
    
    if(hasRootToLeafSum(root,s))//if there exists such a path
        cout<<"A root to leaf path with this sum  exists"<<endl;
    else
        cout<<"No such a path exists"<<endl;
    
    return 0; 

 

Output:

First run:
tree in the example is build here
enter input sum S......
26
A root to leaf path with this sum exists
Second run:
tree in the example is build here
enter input sum S......
8
No such a path exists
Transcribed Image Text:First run: tree in the example is build here enter input sum S...... 26 A root to leaf path with this sum exists Second run: tree in the example is build here enter input sum S...... 8 No such a path exists
Expert Solution
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY