#include<stdio.h>
typedef struct node{
int data;
struct node* left;
struct node* right;
}Node;
void preorder(Node *node){
if(node != NULL){
printf("%d\n",node->data);
preorder(node->left); // 这里注意在if条件内递归 否则陷入NULL死循环
preorder(node->right);
}
}
int main()
{
Node n1;
Node n2;
Node n3;
Node n4;
Node n5;
n1.data=9;
n2.data=8;
n3.data=6;
n4.data=7;
n5.data=5;
// 根 左 右 的顺序
n1.left=&n2;
n1.right=&n3;
n2.left=&n4;
n2.right=&n5;
n3.left=NULL;
n3.right=NULL;
n4.left=NULL;
n4.right=NULL;
n5.left=NULL;
n5.right=NULL;
preorder(&n1); //node是指针,所以传入节点一地址
return 0;
}
原文:https://www.cnblogs.com/rootturing/p/14892112.html