链表和二叉树 指针和双指针
被二叉树定义中的双指针困扰了很久
typedef struct Tree{
char flag;
struct Tree*left;
struct Tree*right;
}tree;
void CreatTree(tree**root) //指向指针的指针 理解为什么要这样
{
char val = 0;
scanf(" %c",&val);
if(val ==*)
{
*root = NULL;
}
else
{
*root = (tree*)malloc(sizeof(tree));
printf("%d
",i);
i++;
if((*root) == NULL)
printf("It is Lost");
else
{
(*root)->flag = val;
printf("root->flag=%c
",(*root)->flag);
CreatTree(&(*root)->left);
CreatTree(&(*root)->right);
}
}
} typedef struct Tree{ char flag; struct Tree*left; struct Tree*right; }tree; void CreatTree(tree**root) //指向指针的指针 理解为什么要这样 { char val = 0; scanf(" %c",&val); if(val ==*) { *root = NULL; } else { *root = (tree*)malloc(sizeof(tree)); printf("%d
",i); i++; if((*root) == NULL) printf("It is Lost"); else { (*root)->flag = val; printf("root->flag=%c
",(*root)->flag); CreatTree(&(*root)->left); CreatTree(&(*root)->right); } } }
在上述二叉树的赋值中用到了双指针,想起以前看过的一个思想,当你函数传递的参数为指针,而你想改变它的值时,你就要用到指针的指针,在二叉树的赋值中,我们用到了递归的思想,而作为参数传递的是tree *root;而我们想改变它的值,所以此时我们要用到指针的指针。
被二叉树定义中的双指针困扰了很久 typedef struct Tree{ char flag; struct Tree*left; struct Tree*right; }tree; void CreatTree(tree**root) //指向指针的指针 理解为什么要这样 { char val = 0; scanf(" %c",&val); if(val ==*) { *root = NULL; } else { *root = (tree*)malloc(sizeof(tree)); printf("%d ",i); i++; if((*root) == NULL) printf("It is Lost"); else { (*root)->flag = val; printf("root->flag=%c ",(*root)->flag); CreatTree(&(*root)->left); CreatTree(&(*root)->right); } } } 在上述二叉树的赋值中用到了双指针,想起以前看过的一个思想,当你函数传递的参数为指针,而你想改变它的值时,你就要用到指针的指针,在二叉树的赋值中,我们用到了递归的思想,而作为参数传递的是tree *root;而我们想改变它的值,所以此时我们要用到指针的指针。下一篇:
二叉树的前、中、后序遍历
