How to calculate size of binary tree

This is continuation of my post How to create binary tree in c# .In this post I will show you how to find size of binary tree.Size of binary tree is number of nodes in a given tree.

Following is the recursive implementation of size function

  //Compute the number of nodes in a tree. 
        public int Size()
            return Size(_root);
        private int Size(Node root)
            if (root == null)
                return 0;
            return 1 + Size(root.Left) + Size(root.Right);

No comments:

Post a Comment