Wednesday, May 11, 2011

Getting the size of Binary Search Tree

This problem demonstrates simple binary tree traversal. Given a binary tree, count the number of nodes in the tree.

Here is the code in c/cpp:
/* Compute the number of nodes in a tree. */
int size(struct node* node) { 
    if (node==NULL) { 
        return(0); 
    } else { 
        return(size(node->left) + 1 + size(node->right)); 
    }
}

0 comments:

Post a Comment