answersLogoWhite

0


Best Answer

int countleaves(struct node* root){

if(root!=null)

{

countleaves(root->left);

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

{

count++;

}

countleaves(root->right);

}

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the Algorithm to count the number of leaf nodes in binary tree?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Count the number of nodes of a binary tree having depth n?

Use the following formula: (2^n)-1. E.g., if the depth is 3, the number of nodes is (2^3)-1 = 8-1 = 7. Note that 7 is the maximum number of nodes, not the actual number of nodes. To count the actual nodes you must traverse the tree, updating an accumulator as you go.


The height of Complete Binary tree is in terms of :option1. n2. logn3. n^2?

The height of a complete binary tree is in terms of log(n) where n is the number of nodes in the tree. The height of a complete binary tree is the maximum number of edges from the root to a leaf, and in a complete binary tree, the number of leaf nodes is equal to the number of internal nodes plus 1. Since the number of leaf nodes in a complete binary tree is equal to 2^h where h is the height of the tree, we can use log2 to find the height of a complete binary tree in terms of the number of nodes.


What is the algorithm to count no of nodes in singly linked list?

int numNodes = 0; Node current = root; // if root is null, the number of nodes is 0 if(current != null) { // if root is not null, we have at least one node numNodes = 1; // count all nodes while(current .next != null) { ++numNodes; current = current.next; } }


Where to insert an element in binary tree if two nodes are equal in size?

Same as if two nodes are NOT equal in size. Size of nodes has nothing to do where to insert a new element. The insertion should be applying the search algorithm of that binary tree (so the new inserted element maybe found later). For balanced (in size) binary tree, the above still applied, because 50% of the time the tree is unbalanced (a binary tree with even number of elements is not balanced). Plus, those 2 nodes, may not be the "right" and "left" nodes of a given one, so 2 nodes equals in size has nothing to do with the way the elements being inserted into a binary tree.


Number of possible binary tree with 3 nodes is?

12


What is the Number of possible binary trees with 3 nodes?

The number of branches is 8.


There are 8 15 13 14 nodes were there in 4 different trees Which of them could have formed a full binary tree?

In general: There are 2n-1 nodes in a full binary tree. By the method of elimination: Full binary trees contain odd number of nodes. So there cannot be full binary trees with 8 or 14 nodes, so rejected. With 13 nodes you can form a complete binary tree but not a full binary tree. So the correct answer is 15. niraj


Find the level of a node in binary tree?

level of a node in any binary tree can be calculated by summing up the number of nodes traversed from the root node of the tree to the node whose level has to be calculated!!!! dats it!! if count is the no. of elements passed, then floor(log2(count-1)) is the level


What is the algorithm to create and count the number of nodes in a single linked list?

Traverse the nodes from the beginning to the end, counting the nodes as you go. This takes linear time O(n) to count n nodes. A more efficient approach that enables constant-time O(1) counting is to encapsulate the list along with a node counter. Increment the counter each time a node is inserted and decrement when a node is extracted.


What is the minimum number of nodes in a binary tree of depth k?

if u assign a 0th level to root of binary tree then,the minimum no. of nodes for depth K is k+1.


What is the number of nodes in a complete binary tree of level 5?

If the number of levels is L, the maximum number of nodes N in a binary tree is N = 2L-1. For L = 5, N equates to 31 thus.


What is number of nodes in a full binary tree which has n levels?

2n-1