我正在瀏覽二叉樹教程。
而我稍微卡住使用遞歸函數。說例如我需要計算樹中的節點數二叉樹中的遞歸函數解釋
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-> right-> left-> left? 由於
另請參見[樹結構和計算機程序的解釋]遞歸(http://mitpress.mit.edu/sicp/full-text/book/book-ZH-11.html#%_sec_1.2.2) – outis