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;
}