繼計數器變量是2碼: 1.查找二叉查找樹的第k個最小整數:遞歸和在二叉樹
void FindKthSmallest(struct TreeNode* root, int& k)
{
if (root == NULL) return;
if (k == 0) return; // k==0 means target node has been found
FindKthSmallest (root->left, k);
if (k > 0) // k==0 means target node has been found
{
k--;
if (k == 0) { // target node is current node
cout << root->data;
return;
} else {
FindKthSmallest (root->right, k);
}
}
}
查找節點的二進制樹編號:
int Size (struct TreeNode* root) { if (root == NULL) return 0; int l = Size (root->left); int r = Size (root->right); return (l+r+1); }
我的問題: 在這兩個代碼,我會跟蹤我訪問節點的數量。爲什麼是它的代碼1,需要通過引用傳遞一個參數來跟蹤我訪問節點的數量,而代碼2不要求按引用傳遞任何變量?