The code is not printing the correct output please help

Given a Binary Tree Print all the leaf nodes.
#include

using namespace std;

class Node

{

public:

int data;

Node *left;

Node *right;

Node(int d)

{

    data = d;

    left = NULL;

    right = NULL;

}

};

Node *create()

{

int d;

cin >> d;

if (d == -1)

{

    return NULL;

}

Node *root = new Node(d);

root->left = create();

root->right = create();

return root;

}

void printleafNodes(Node *root)

{

if (root == NULL)

{

    return;

}

if (root->left == NULL and root->right == NULL)

{

    cout << root->data << " ";

}

if (root->left)

{

    printleafNodes(root->left);

}

if (root->right)

{

    printleafNodes(root->right);

}

}

int main()

{

Node *r=new Node(0);

r=create();

printleafNodes(r);

return 0;

}

hi @guptaharish93_ea50700f6731bd2b
ur buildtree function was not correct… i have made changes in ur code… its working fine now…
corrected code --> https://ide.codingblocks.com/s/642854

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.