Thursday, September 29, 2022

Data Structures and Algorithms (Some Questions for You - Answer to Question 9) - Tirthankar Pal - MBA from IIT Kharagpur, GATE, GMAT, IIT Written Test, Interview were a part of MBA Entrance, B.S. in Computer Science from NIELIT

 In a complete binary tree of depth d (complete including last level), give an expression to find the number of leaf nodes in the binary tree.

A node is a leaf node if both left and right child nodes of it are NULL. 
Here is an algorithm to get the leaf node count.

getLeafCount(node)
1) If node is NULL then return 0.
2) Else If left and right child nodes are NULL return 1.
3) Else recursively calculate leaf count of the tree using below formula.
    Leaf count of a tree = Leaf count of left subtree + 
                                 Leaf count of right subtree

Example Tree

Leaf count for the above tree is 3.


Output: 

The leaf count of binary tree is : 3

Time & Space Complexities: Since this program is similar to traversal of tree, time and space complexities will be same as Tree traversal 


Tirthankar Pal

MBA from IIT Kharagpur with GATE, GMAT, IIT Kharagpur Written Test, and Interview

2 year PGDM (E-Business) from Welingkar, Mumbai

4 years of Bachelor of Science (Hons) in Computer Science from the National Institute of Electronics and Information Technology

Google and Hubspot Certification

Brain Bench Certification in C++, VC++, Data Structure and Project Management

10 years of Experience in Software Development out of that 6 years 8 months in Wipro

Selected in Six World Class UK Universities:-

King's College London, Durham University, University of Exeter, University of Sheffield, University of Newcastle, University of Leeds


No comments:

Post a Comment