• 主页
• 问答

# 问一个二叉树遍历的问题

jianzhibeihang 2010-07-12 09:28:22

``````typedef struct node{
int data;
struct node *left;
struct node *right;
struct node *parent;
}Node;``````

...全文
127 点赞 收藏 11

11 条回复

jianzhibeihang 2010-07-13
[Quote=引用 9 楼 chj_2003 的回复:]

[/Quote]

chj_2003 2010-07-13

jianzhibeihang 2010-07-13
[Quote=引用 7 楼 chj_2003 的回复:]
#include <stdio.h>
#include <stdlib.h>

typedef struct node{
int data;
struct node *left;
struct node *right;
struct node *next;
}Node;

void incu……
[/Quote]

chj_2003 2010-07-13

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
int data;
struct node *left;
struct node *right;
struct node *next;
}Node;

void incursive(Node *root)
{
Node *p = root;
if(root == NULL)
return;

printf("%d\n",p->data);
incursive(p->left);
incursive(p->right);
}

int main()
{
Node *p1,*p2,*p3,*p4,*p5,*root;
root = (Node *)malloc(sizeof(Node));
p1 = (Node *)malloc(sizeof(Node));
p2 = (Node *)malloc(sizeof(Node));

root->data = 0;
root->left = p1;
root->right = p2;
root->next = NULL;

p1->data = 1;
p1->left = NULL;
p1->right = p3;
p1->next = root;

p2->data = 2;
p2->left = NULL;
p2->right = NULL;
p2->next = root;

p3->data = 3;
p3->left = p4;
p3->right = p5;
p3->next = p1;

p4->data = 4;
p4->left = NULL;
p4->right = NULL;
p4->next = p3;

p5->data = 5;
p5->left = NULL;
p5->right = NULL;
p5->next = p3;

incursive(root);

free(root);
free(p1);
free(p2);
free(p3);
free(p4);
free(p5);

system("pause");
return 0;
}

chj_2003 2010-07-13
if(p == NULL)
printf("Fuck\n")；哥们，你这句很有意思。

jianzhibeihang 2010-07-13
[Quote=引用 4 楼 pmars 的回复:]

C/C++ code

Node *FindLeft(Node *p)
{
if(p == NULL)
return NULL;
if(p->left != NULL)
p = p->left;
return p;
}

void incursive(Node *root)
{
Node *p = ro……
[/Quote]

pmars 2010-07-13
``````
Node *FindLeft(Node *p)
{
if(p == NULL)
return NULL;
if(p->left != NULL)
p = p->left;
return p;
}

void incursive(Node *root)
{
Node *p = root;
if(root == NULL)
return;
if(root->left == NULL && root->right == NULL)
printf("%d\n",root->data);

if(p->left == NULL)
{
printf("%d\n",p->data);
p = p->right;
p = FindLeft(p);
}
else{
//     printf("Hello1\n");
p = FindLeft(p);  //第一次执行到这里的时候p被赋值为p1
}
/*
if(p == NULL)
printf("Fuck\n");
*/
while(p != NULL)//之后执行这里
{
//    printf("Hello2!\n");
printf("%d\n",p->data);
if(p->right != NULL）//
incursive(p->right);//p1又右子树，所以执行，但是始终没有更改p的值，导致每次while循环都TRUE，死了就
else
{
printf("%d\n",p->data);
p = p->next;
if(p == root)
break;
}
}
}

``````

chj_2003 2010-07-13
[Quote=引用 10 楼 jianzhibeihang 的回复:]

[/Quote]
if(p->next->left==p)左孩子；if(p->next->right==p)右孩子；推到根节点知道路径了

budweiser 2010-07-12

budweiser 2010-07-12

jianzhibeihang 2010-07-12

``````
#include <stdio.h>
#include <stdlib.h>

typedef struct node{
int data;
struct node *left;
struct node *right;
struct node *next;
}Node;

Node *FindLeft(Node *p)
{
if(p == NULL)
return NULL;
if(p->left != NULL)
p = p->left;
return p;
}

void incursive(Node *root)
{
Node *p = root;
if(root == NULL)
return;
if(root->left == NULL && root->right == NULL)
printf("%d\n",root->data);

if(p->left == NULL)
{
printf("%d\n",p->data);
p = p->right;
p = FindLeft(p);
}
else{
//     printf("Hello1\n");
p = FindLeft(p);
}
/*
if(p == NULL)
printf("Fuck\n");
*/
while(p != NULL)
{
//    printf("Hello2!\n");
printf("%d\n",p->data);
if(p->right != NULL)
incursive(p->right);
else
{
printf("%d\n",p->data);
p = p->next;
if(p == root)
break;
}
}
}

int main()
{
Node *p1,*p2,*p3,*p4,*p5,*root;
root = (Node *)malloc(sizeof(Node));
p1 = (Node *)malloc(sizeof(Node));
p2 = (Node *)malloc(sizeof(Node));
p3 = (Node *)malloc(sizeof(Node));
p4 = (Node *)malloc(sizeof(Node));
p5 = (Node *)malloc(sizeof(Node));

root->data = 0;
root->left = p1;
root->right = p2;
root->next = NULL;

p1->data = 1;
p1->left = NULL;
p1->right = p3;
p1->next = root;

p2->data = 2;
p2->left = NULL;
p2->right = NULL;
p2->next = root;

p3->data = 3;
p3->left = p4;
p3->right = p5;
p3->next = p1;

p4->data = 4;
p4->left = NULL;
p4->right = NULL;
p4->next = p3;

p5->data = 5;
p5->left = NULL;
p5->right = NULL;
p5->next = p3;

incursive(root);

free(root);
free(p1);
free(p2);
free(p3);
free(p4);
free(p5);

system("pause");
return 0;
}

``````

3.2w+