I want to count the no of leaf nodes: Note:Cannot use global/class level variable I implmeted following algo, and it works fine.But i want method signature to be
countLeaves(Node node)
I know that i can overload methds and call the 2 args method sig from 1 args, but dont want to do so.Can anyone suggest any other method?
int countLeaves(Node node,int count){
if(node==null)
return 0;
if(node.left==null && node.right==null){
return 1+count;
}else{
int lc = countLeaves(node.left, count);
int total = countLeaves(node.right, lc);
return total;
}
}
int countLeaves(Node node){
if( node == null )
return 0;
if( node.left == null && node.right == null ) {
return 1;
} else {
return countLeaves(node.left) + countLeaves(node.right);
}
}
You are doing the same thing as before but instead of holding the current count as we go, we simply say return the result of the sum of the left and right node. These in turn recurse down till they hit the basecases.
You don't need to pass count down the call stack, only up from:
int countLeaves(Node node)
{
if(node==null) {
return 0;
}
if(node.left==null && node.right==null) {
return 1;
}
return countLeaves(node.left) + countLeaves(node.right);
}
We can apply two approaches , one is Recursive and other one is iterative(queue based implementation ) .Here i am going to explain both methods.
Recursive Solution
int count_leaf(Node node)
{
if(node==NULL)
return 0;
if(node->left==NULL && node->right==NULL)
return 1;
return count_leaf(node->left)+count_leaf(node->right);
}
Second Method is Iterative(Queue based implementation ) , idea is taken from level order traversal of a tree.
int count_leaf(Node root)
{
int count=0;
if(root==NULL)
return 0;
queue<Node *> myqueue;
myqueue.push(root);
while(!myqueue.empty())
{
Node temp;
temp=myqueue.top(); //Take the front element of queue
myqueue.pop(); //remove the front element of queue
if(temp->left==NULL && temp->right==NULL)
count++;
if(temp->left)
myqueue.push(temp->left);
if(temp->right)
myqueue.push(temp->right);
}
return count;
}
I hope these solutions will help you.
Fill in ??? part yourself.
int countLeaves(Node node){
if (node==null)
return 0;
if (node.left==null && node.right==null){
return 1;
} else {
int lc = countLeaves(node.left);
int rc = countLeaves(node.right);
return ???;
}
}
As I am still unit testing this implementation, no promises of bug-free operation are made. If there are particular issues with the implementation, I am happy to receive the feedback.
Initially, I am receiving favorable results:
#region CountNode utility
private int CountNode(Node node, Direction d) {
Func<Direction, Node> leaf = (dir) => {
return dir == Direction.Left ? node.Left : node.Right;
};
var done = leaf(d) == null;
var root = node;
var stack = new Stack<Node>( );
var count = 0;
while(!done) {
if (node != null) {
stack.Push(node);
node = leaf(d);
}
else {
if(stack.Count > 0) {
node = stack.Pop( );
// back to root, time to quit
if(node == root) {
done = true;
continue;
}
// count nodes when popped
++count;
// flip direction
var flip = d == Direction.Left ? Direction.Right : Direction.Left;
// get the leaf node
node = leaf(flip);
}
else {
done = true;
}
}
}
return count;
}
#endregion
Usage:
var actLeftCount = CountNode(root, Direction.Left);
var actRightCount = CountNode(root, Direction.Right);
This has the particular advantage of giving counting only the nodes on the Left. I can pass any node into the method and receive statistics for the same.
Same as in the first comment.
int getLeafNodeCount(Node<T> node) {
if(node == null) {
return 0;
}
if (node.leftChild == null && node.rightChild == null) {
return 1;
} else {
return getLeafNodeCount(node.leftChild) + getLeafNodeCount(node.rightChild);
}
}
public int getLeafsCount(BSTNode<T> node, int count) {
if(node == null || node.getData() == null){
return count;
}
else if(isLeaf(node)){
return ++count;
}else{
int leafsLeft = getLeafsCount((BSTNode<T>) node.getLeft(), count);
int leafsCount = getLeafsCount((BSTNode<T>) node.getRight(), leafsLeft);
return leafsCount;
}
}
来源:https://stackoverflow.com/questions/5949882/counting-number-of-leaf-nodes-in-binary-tree