JIGYASA

An online placement forum.


You are not connected. Please login or register

Leaf nodes ?

Go down  Message [Page 1 of 1]

1 Leaf nodes ? on 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...
}

View user profile http://jigyasa.forumn.org

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

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

View user profile

Back to top  Message [Page 1 of 1]

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