请问为什么访问 3 号叶子的路径不能正确返回呢
#include "stdafx.h"#include <stdio.h>
#include <string>
using std::string;
struct Node
{
Node *childl=NULL, *childr=NULL, *father=NULL;
int data, flag=0;
};
Node* Search(Node *node, int n)
{
Node *p = NULL;
if (node->data == n)
{
node->flag = 1;
printf("%d",node->data);
return node;
}
if (node->childl != NULL)
{
p = Search(node->childl, n);
if (p != NULL)
{
node->flag++;
printf("%d", node->data);
return p;
}
}
if (node->childr != NULL)
{
p = Search(node->childr, n);
if (p != NULL)
{
node->flag++;
printf("%d", node->data);
return p;
}
}
if (node->childl == NULL&&node->childr == NULL)
{
return NULL;
}
}
void Jump(Node *node, int n[], int i, int j)//j是界限
{
Node *p = NULL;
p = Search(node, n[i]);
if (p != NULL)
{
i=i+1;
if (i <j)
{
//printf("%d", p->father->data);
Jump(p->father, n, i, j);
}
else if(i>=j)
{
return;
}
}
if (p == NULL)
{
if (node->father != NULL)
{
//printf("%d", node->data);
Jump(node->father, n, i, j);
return;
}
if (node->father == NULL)
{
return;
}
}
}
void Add(Node* p1,Node*p2, int n,Node* target,Node *p3)
{
target->childl = p1;
target->childr = p2;
target->data = n;
target->father=p3;
}
int _tmain(int argc, _TCHAR* argv[])
{
int n[2];
n[0] = 5;
n[1] = 3;
Node a, b, c, d, e,f;
Add(&b,&c,1,&a,NULL);
Add(&d,NULL, 2, &b,&a);
Add(&e, &f, 4, &d,&b);
Add(NULL, NULL, 5, &e,&d);
Add(NULL, NULL, 6, &f,&d);
Add(NULL, NULL, 3, &c,&a);
Node *p = &a;
Jump(p, n, 0, 2);
scanf_s("%d",n,1);
return 0;
}