【发布时间】:2016-07-20 10:43:12
【问题描述】:
bool roottoleafsumequaltox(BinaryTreenode<int>* root, int &x)
{
if(root == NULL)
{
return (x==0);
}
else
{
bool ans = false;
x = x - root->data;
if(x == 0 && root->left == NULL && root->right == NULL)
{
return true;
}
if(root->left)
ans = ans || roottoleafsumequaltox(root->left, x);
if(root->right)
ans = ans || roottoleafsumequaltox(root->right, x);
return ans;
}
}
它必须返回根到叶的总和是否等于给定的数字 x。我认为问题在于通过引用传递,我无法检测到它...... 它总是给出错误的答案,即使它是真的!
【问题讨论】:
标签: c++ c++11 boolean binary-tree pass-by-reference