Find the Size of the Binary Tree

Objective: Given a Binary tree, Find the size of the tree.

Note : Size of the tree is number of nodes in the tree

Input: A Binary Tree.

Output: Size of the tree.

Example :

Size-of-Binary-Tree-example

Approach :

  • Very Simple solution
  • Start from the root.
  • Size = 1 (for the root) + Size Of left Sub-Tree + Size Of right Sub-Tree
  • solve the left sub-tree and right sub-tree recursively.

Time Complexity : O(n)

Code:

Output:

Size of the Tree is : 7

4 Responses

  1. XIANG ZUO says:

    1+3+3=8, nice try

  2. lipsa patel says:

    Both of the images represent wrong size 8 instead of 7

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: