Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Find the Size of the Binary Tree

Objec­tive: Given a Binary tree, Find the size of the tree.

Note : Size of the tree is num­ber of nodes in the tree

Input: A Binary Tree.

Out­put: Size of the tree.

Exam­ple :

Size of Binary Tree example

Size of Binary Tree example

Approach :

  • Very Sim­ple 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.
Size of Binary Tree Implement

Size of Binary Tree Implement

Time Com­plex­ity : O(n)

Com­plete Code:


Out­put:

Size of the Tree is : 7

You may also like...

  • XIANG ZUO

    1+3+3=8, nice try