C++ 找到树的按序遍历,并通过对每个备用数字求反来打印它们

C++ 找到树的按序遍历,并通过对每个备用数字求反来打印它们,c++,binary-tree,inorder,C++,Binary Tree,Inorder,例如: 1 / \ / \ 2 3 / \ / \ 4 5 6 7 顺序遍历输出:4 2 5 1 6 3 7 预期产出:4-25-16-37 inorder的代码是 Node * func(struct Node * root){ if(root!=NULL) { func(root->lChild); cout<<root->nodeV

例如:

         1
      /      \
    /         \
  2             3
 /  \         / \
 4   5       6   7
顺序遍历输出:4 2 5 1 6 3 7

预期产出:4-25-16-37

inorder的代码是

Node * func(struct Node * root){
if(root!=NULL)
{
func(root->lChild);
cout<<root->nodeValue<<" ";
func(root->rChild);
}
return NULL;
Node*func(结构节点*根){
if(root!=NULL)
{
func(root->lChild);

cout您可能需要做的就是添加一个额外的参数来跟踪替代符号,如下所示:

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;
}
Node*func(结构节点*root,int&signV){
if(root!=NULL)
{
func(root->lChild,signV);

我们可以假设你不能把所有的项目都列在一个列表中然后否定,对吗?问题是什么?你的标题读起来就像你没有问的问题的答案,就像@alseether说的,简单地先得到所有的数字,然后用交替的符号打印出来。祝你的家庭作业好运,但如果你真的想得到帮助,你应该把你尝试过的东西贴出来,然后他正是你遇到的问题。