劍指offer(六)

1.反轉鏈表

#include <iostream>
using namespace std;
struct List
{
	int data;
	List* next;
	List(int num) :data(num),next(NULL){};
};
List* init_list(int *a,int n)
{
	List *head=new List(a[0]);
	List *q=head;
	for (int i=1;i<n;i++)
	{
		List *p=new List(a[i]);
		q->next=p;
		q=p;
	}
	return head;

}
void reverse_list(List * &head)//注意點,要加引用
{
	if(!head) return;//注意點
	List *p=head,*q,*r;
	if(head->next) q=head->next;
	if(head->next->next) r=head->next->next;
	p->next=NULL;
	while(r)
	{
		q->next=p;
		if(r) ////注意點
		{
			p=q;
			q=r;
		}
		r=r->next;//注意點
	}
	q->next=p;
	head=q;
}
int main()
{
	int a[]={0,1,2,3,4};
	int len=sizeof(a)/sizeof(a[0]);
	List *head=init_list(a,len);
	reverse_list(head);
	return 0;
}
2.合併排序(遞增)鏈表(結果還是遞增的)

#include <iostream>
using namespace std;
struct List
{
	int data;
	List* next;
	List(int num) :data(num),next(NULL){};
};
List* init_list(int *a,int n)
{
	List *head=new List(a[0]);
	List *q=head;
	for (int i=1;i<n;i++)
	{
		List *p=new List(a[i]);
		q->next=p;
		q=p;
	}
	return head;

}
List* merge_sort_list(List * head1,List* head2)
{
	if (head1==NULL) return head2;
	else if (head2==NULL) return head1;
	List *temp=NULL;
	if(head1->data>head2->data)
	{
		temp=head2;
		temp->next=merge_sort_list(head1,head2->next);
	}
	else
	{
		temp=head1;
		temp->next=merge_sort_list(head1->next,head2);
	}
	return temp;
}
int main()
{
	int a[]={0,2};
	int b[]={1,3,5};
	int len=sizeof(a)/sizeof(a[0]);
	List *head1=init_list(a,len);
	List *head2=init_list(b,len+1);
	List *merge_list=merge_sort_list(head1,head2);
	return 0;
}
3.判斷是否是子樹(兩棵樹A,B,判斷是否B是A的子樹)

#include <iostream>
#include <vector>
using namespace std;
struct Btree
{
	int data;
	Btree* left,*right;
	Btree(int num) :data(num),left(NULL),right(NULL){};
};
void  Find_The_root(Btree *A,Btree *B,int ×,Btree * &root)
{
	if(A&&B) 
	{
		if (A->data==B->data)
			if (!times)
			{
				root=A;
				times++;
			}
				
		Find_The_root(A->left,B,times,root);
		Find_The_root(A->right,B,times,root);
	}
}
bool Is_same(Btree * A,Btree *B)
{
	if(!B) return true;//返回條件
	if(!A||A->data!=B->data) return false;
	return(Is_same(A->left,B->left)&&Is_same(A->right,B->right));
}
void Is_child(Btree * A,Btree *B,bool &reslut)
{
	int times=0;
	Btree * temp_tree=NULL;
	Find_The_root(A,B,times,temp_tree);
	if(temp_tree)
	{
		if(!Is_same(temp_tree,B)) 
		{
			Is_child(temp_tree->left,B,reslut);
			Is_child(temp_tree->right,B,reslut);
		}
		else reslut=true;
	}
}
Btree* init_Btree(int *a,int n)
{
	vector<Btree *> v1;
	for (int i=0;i<n;i++)
	{
		Btree * p=new Btree(a[i]);
		v1.push_back(p);
	}
	for (int i=0;i<=n/2-1;i++)
	{
		v1[i]->left=v1[2*i+1];
		if(2*i+2<=n-1)
			v1[i]->right=v1[2*i+2];
	}
	return v1[0];
}

int main()
{
	int a[]={2,2,3,4,5,6};
	int b[]={2,4,5};
	int len=sizeof(a)/sizeof(a[0]);
	Btree *A=init_Btree(a,len);
	len=sizeof(b)/sizeof(b[0]);
	Btree *B=init_Btree(b,len);
	bool result=false;
	Is_child(A,B,result);
	cout<<result;
	return 0;
}
4.二叉樹鏡像

#include <iostream>
#include <vector>
using namespace std;
struct Btree
{
	int data;
	Btree* left,*right;
	Btree(int num) :data(num),left(NULL),right(NULL){};
};
void Mirror_Btree(Btree * &root)
{
	if(!root||(root->right==NULL&&root->left==NULL)) return ;
	Btree *temp=root->left;
	root->left=root->right;
	root->right=temp;
	Mirror_Btree(root->left);
	Mirror_Btree(root->right);
}
Btree* init_Btree(int *a,int n)
{
	vector<Btree *> v1;
	for (int i=0;i<n;i++)
	{
		Btree * p=new Btree(a[i]);
		v1.push_back(p);
	}
	for (int i=0;i<=n/2-1;i++)
	{
		v1[i]->left=v1[2*i+1];
		if(2*i+2<=n-1)
			v1[i]->right=v1[2*i+2];
	}
	return v1[0];
}

int main()
{
	int a[]={1,2,3,4,5,6};
	int len=sizeof(a)/sizeof(a[0]);
	Btree *A=init_Btree(a,len);
	Mirror_Btree(A);
	
	return 0;
}






發佈了67 篇原創文章 · 獲贊 4 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章