【发布时间】:2011-11-04 08:58:40
【问题描述】:
我正在阅读二叉树的教程。
而且我在使用递归函数时有点卡住了。比如说我需要计算树中的节点数
int countNodes( TreeNode *root )
{
// Count the nodes in the binary tree to which
// root points, and return the answer.
if ( root == NULL )
return 0; // The tree is empty. It contains no nodes.
else
{
int count = 1; // Start by counting the root.
count += countNodes(root->left); // Add the number of nodes
// in the left subtree.
count += countNodes(root->right); // Add the number of nodes
// in the right subtree.
return count; // Return the total.
}
} // end countNodes()
现在我的疑问是-> root->left->left of right 怎么算?或root->right->left->left?? 谢谢
【问题讨论】:
标签: c++ c binary-tree