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

来源:互联网 发布:suse linux下载 编辑:程序博客网 时间:2024/05/20 09:05

什么是二叉树?

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

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

中序+前序  求 后序

//根据前序,中序,建立树,输出后序遍历的结果 #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; }

原创粉丝点击