求解二叉树的节点数量(递归方法)

2015-02-28  本文已影响16人  lintong

Size() function recursively calculates the size of a tree. It works as follows:

Size of a tree = Size of left subtree + 1 + Size of right subtree

/* Computes 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)); 
}
上一篇下一篇

猜你喜欢

热点阅读