二叉树的前序,中序,后序遍历

什么是二叉树?

二叉树是每个节点最多有两个子树的树结构。通常子树被称作“左子树”和“右子树”,左子树和右子树同时也是二叉树。二叉树的子树有左右之分,并且次序不能任意颠倒。

因为忘记了关于树的遍历,所以现在做下笔记。

中序+前序  求 后序

//根据前序,中序,建立树,输出后序遍历的结果 
#include<stdio.h>
#include<stdlib.h>
struct Node
{
	Node *lchild;
	Node *rchild;
	int data;
};
Node * divide(int *in,int *pre,int len)
{
	if(len==0)
	{
		return NULL;
	}
	Node * root = (Node *)malloc(sizeof(Node));
	root->data = pre[0];
	int k=0;
	for(;k<len;k++)
	{
		if(in[k]==pre[0])
		{
			break;
		}
	}
	root->lchild=divide(in,pre+1,k);
	root->rchild=divide(in+k+1,pre+k+1,len-(k+1));
	printf("%d",root->data);
	return root;
} 
void dfs(Node * root,int sum)
{
	if(root!=NULL)
	{
		if(root->lchild == NULL && root->rchild == NULL)
		{
			printf("叶子节点:%d\n",root->data);
		}
		if(root->lchild!=NULL){
			dfs(root->lchild,sum+root->data);
		}
		if(root->rchild!=NULL){
			dfs(root->rchild,sum+root->data);
		}
	}else{
		return ;
	}
}
int main()
{
	const int size = 7;
	int pre[size]={4,2,3,1,7,5,6};
	int in[size]={3,2,1,4,5,7,6};
	
	printf("后序遍历:");
	Node * root = divide(in,pre,size);
	printf("\n");
	dfs(root,0);
        return 0;
}

中序+后序  求 前序

//根据中序,后序 建立树,输出前序遍历结果
#include<stdio.h>
#include<stdlib.h>
#include<string>
using namespace std;
struct Node
{
	Node *lchild;
	Node *rchild;
	int data;
	
};
Node * build(int* in,int* hou,int len)
{
	if(len==0)
	{
		return NULL ;
	}
	Node * root = (Node *) malloc(sizeof(Node));
	root->data = hou[len-1];
	printf("%d",root->data);
	int k=0;
	for(;k<len;k++)
	{
		if(in[k]==hou[len-1])
		{
			break;
		}
	}
	root->lchild=build(in,hou,k);
	root->rchild=build(in+k+1,hou+k,len-(k+1));
	return root;
}
void dfs(Node * root,int sum)
{
	if(root!=NULL)
	{
		if(root->lchild == NULL && root->rchild == NULL)
		{
			printf("叶子节点:%d\n",root->data);
		}
		if(root->lchild!=NULL){
			dfs(root->lchild,sum+root->data);
		}
		if(root->rchild!=NULL){
			dfs(root->rchild,sum+root->data);
		}
	}else{
		return ;
	}
}
int strlen(char * from)
{
	int i=0;
	while(from[i]!='\0')
	{
		i++;
	}
	return i;
}
int main()
{
	const int size = 7;
	int in[size]={3,2,1,4,5,7,6};
	int hou[size]={3,1,2,5,6,7,4};
	printf("前序遍历:");
	Node * root = build(in,hou,size);
	printf("\n"); 
	dfs(root,0);
	return 0;
}

练习: https://vjudge.net/problem/UVA-548

#include<stdio.h>
#include<stdlib.h>
#include<string>
using namespace std;
int m = 10000*10000;
int rs;
struct Node
{
	Node *lchild;
	Node *rchild;
	int data;
};
Node* build(int *in,int *hou,int len)
{
	if(len==0)
	{
		return NULL;
	}
	Node *root = (Node*)malloc(sizeof(Node));
	root->data = hou[len-1];
	int k =0;
	for(;k<len;k++)
	{
		if(in[k]==hou[len-1])
		{
			break;
		}
	}
	root->lchild = build(in,hou,k);
	root->rchild = build(in+k+1,hou+k,len-(k+1));
	return root;
}
void dfs(Node * root, int sum) //遍历树
{
	if(root!=NULL)
	{
		if(root->lchild == NULL && root->rchild==NULL)
		{
			if(m>sum+root->data)
			{
				m=sum+root->data;
				rs=root->data;
			}
		}
		if(root->lchild!=NULL)
		{
			dfs(root->lchild,sum+root->data);
		}
		if(root->rchild!=NULL)
		{
			dfs(root->rchild,sum+root->data);
		}
	}else{
		return ;
	}
}
int main()
{
	const int size = 10010;
	char str[size*2];
	int in[size * 2];
	while(gets(str))
	{
		m = 10000*10000;
//		getchar();
		string str1(str);
		gets(str);
		string str2(str);
		str1=str1+" "+str2;
		
		int len = str1.length();
		int s = 0;
		for(int i=0;i<len;i++)
		{
			int tmp = 0;
			while(str1[i]!=' ' && i<len)
			{
				tmp = tmp * 10 + str1[i]-'0';;
				i++;
			}
			in[s]=tmp;
			s++;	
		}
		Node * root = build(in,in+s/2,s/2);
		dfs(root,0);
		printf("%d\n",rs);
	}
       return 0;
 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值