找到树的无序遍历,并通过否定每个备用数字来打印它们
find the inorder traversal of the tree and print them all by negating every alternate number
例如:
1
/
/
2 3
/ /
4 5 6 7
顺序遍历输出:4 2 5 1 6 3 7
预期产量: 4 -2 5 -1 6 -3 7
Inorder 的代码是
Node * func(struct Node * root){
if(root!=NULL)
{
func(root->lChild);
cout<<root->nodeValue<<" ";
func(root->rChild);
}
return NULL;
}
您可能要做的就是 ,添加一个额外的参数来跟踪备用符号,如下所示:
Node * func(struct Node * root, int& signV ){
if(root!=NULL)
{
func(root->lChild, signV);
cout<<root->nodeValue * signV <<" "; signV *= -1 ; // Change sign here
func(root->rChild, signV);
}
return NULL;
}
See Here
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 检查输入是否不是整数或数字
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 如何用数字处理log(0)
- 最高有效数字侧的第N位
- 如何获取一个数字的前3位
- 查找最接近的大于当前数字的数字的索引
- 找到两对数字,使它们的乘积的绝对差最小化
- 我想做一个彼此不同但重复出现的数字
- 将数字转换为字母(例如:123 转换为一二三)
- C++如何计算用户输入的数字中的偶数位数
- 如何在C++中确定文本文件中的元素是字符还是数字
- 打印数字图案
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 如何检查一个c++字符串中有多少相同的字符/数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 将数字打印成文字
- 找到树的无序遍历,并通过否定每个备用数字来打印它们
- 在列表<int>或向量<int>中添加备用数字