#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct Node{
int data;
Node* left;
Node* right;
};
Node* new_node(int data1)
{
Node* new_node=(Node*)malloc(sizeof(Node));
new_node->data=data1;
new_node->left=NULL;
new_node->right=NULL;
return new_node;
}
void Display(Node* root)
{
if(root!=NULL)
{//printf("\nhi123\n");
Display(root->left);
printf("%d ",root->data);
Display(root->right);
}
}
void convertTreeToDll(Node* root,Node** head)
{
if(root==NULL)
return ;
if(root->left==NULL && root->right==NULL)
{
if(*head==NULL)
{
*head=new_node(root->data);
printf("\nhi1234\n");
}else
{
Node* head1=*head;
printf("\nhello head1->data=%d\n",head1->data) ;
Node* temp=new_node(root->data);
cout<<"temp adress="<<temp<<" root address="<<root<<" head1="<<head1<<endl;
head1->left=temp;
temp->right=head1;
head1=temp;
cout<<"temp adress="<<temp<<" root address="<<root<<" head1="<<head1<<endl;
}
}
convertTreeToDll(root->left,head);
convertTreeToDll(root->right,head);
}
void DoubleLinkList(Node* root)
{
if(root!=NULL)
{
printf("%d ",root->data);
root=root->right;
}
}
void convertUtil(Node* root)
{
printf("\ndata=%d\n",root);
Node* head=NULL;
convertTreeToDll(root,&head);
//while(head->left!=NULL)
//head=head->left;
DoubleLinkList(head);
}
void change(Node** root1)
{
Node* root=*root1;
cout<<" root="<<root<<" *root1="<<*root1<<endl;
if(root==NULL)
return;
//printf("\nhi\n");
change(&(root->left));
change(&(root->right));
free(root);
root=NULL;
}
int main()
{
Node* root=new_node(1);
root->left=new_node(2);
root->right=new_node(3);
Display(root);
change(&root);
//cout<<endl<<"root->left="<<root->left->data<<endl;
//root->left=new_node(20);
Display(root);
Node* head=NULL;
// convertTreeToDll(root,&head);
//printf("\nhead->data=%d\n",head);
//convertUtil(root);
return 0;
}
当我设置*root1 =NULL
时,在更改功能中它正常工作。但是当我设置root=NUL
L时,当程序控制移动到显示功能时它不会变为NULL。请帮忙。
当我设置root=NULL
时输出为:
2 1 3
0 34979904 34979872