1.

Write an algorithm for counting the number of leaf nodes in a binary tree.

Answer»

An algorithm for counting the number of leaf nodes in a binary tree is given below:

  • STEP 1: If the current node is null, RETURN a value 0.
  • Step 2: If a leaf node is encountered, that is, if the current node's left and RIGHT nodes are both null, then return 1.
  • Step 3: Calculate the number of leaf nodes recursively by adding the number of leaf nodes in the left subtree by the number of leaf nodes in the right subtree.


Discussion

No Comment Found