Write a C Program to count leaf nodes in a binary tree

A node is a leaf node if both left and right child nodes of it are NULL. air max 90 pas cher Algorithm :

 1) If node is NULL then return 0.  <a href="http://www.sintel-fi.fr/adidas-chaussures-femme.html">adidas chaussures femme</a> 2) Else If left and right child nodes are NULL return 1.  <a href="http://www.sacfjallravenkanken.fr/fjallraven-kanken-mini.html">Fjallraven Kanken Mini</a></li> 3) Else recursively calculate leaf count of the tree using below formula.  <a href="http://www.saintmartinairmodeles.fr/stats/best/">nike tn rouge</a> Leaf count of a tree = Leaf count of left subtree + Leaf count of right subtree 

Code :

 /* Function to get the count of leaf nodes in a binary tree*/ int getLeafCount(struct node* node) { if(node == NULL) return 0; if(node->left == NULL && node->right==NULL) return 1; else return getLeafCount(node->left) + getLeafCount(node->right); }

Time and Space Complexities: Above program is similar to traversal of tree,

Leave a Reply

Your email address will not be published. Required fields are marked *

For Inserting code :
Paste your code in the comment form, select it and then click the language link

C | C++ | Java |