JIGYASA
Would you like to react to this message? Create an account in a few clicks or log in to continue.
JIGYASA

An online placement forum.


You are not connected. Please login or register

Leaf nodes ?

2 posters

Go down  Message [Page 1 of 1]

1Leaf nodes ? Empty Leaf nodes ? Tue Jun 22, 2010 3:29 am

Lucifer


Admin

Given a binary tree. Write a function which takes the root of the tree and returns the number of leaf nodes present in the binary tree.

struct node
{
int data;
struct node * lChild;
struct node * rChild;
};

int leafNodes(node *root)
{
// Put your code here...
}

https://jigyasa.forumotion.com

2Leaf nodes ? Empty Re: Leaf nodes ? Tue Sep 21, 2010 5:35 am

codecrker



int leafNodes(node *root)
{
return root ? ((root->lChild + root->rChild) ? leafNodes(root->lChild) + leafNodes(root->rChild) : 1) : 0;
}

/* ---------- */

int leafNodes(node *root)
{
if(!root)
return 0;
else
return (root->lChild + root->rChild) ? leafNodes(root->lChild) + leafNodes(root->rChild) : 1;
}

/* ---------- */

int leafNodes(node *root)
{
if(!root)
return 0;
else
return (root->lChild == root->rChild) ? 1 : leafNodes(root->lChild) + leafNodes(root->rChild);
}

Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum