剑指Offer-- 重建二叉树 前序中序建立二叉树
来源:互联网 发布:python 绝技 源码 编辑:程序博客网 时间:2024/06/18 11:48
剑指Offer-- 重建二叉树 前序中序建立二叉树
AC代码:
package algorithm;import common.TreeNode;/** * 类说明 * * <pre> * Modify Information: * Author Date Description * ============ =========== ============================ * DELL 2017年5月17日 Create this file * </pre> * */public class ReConstructBinaryTree { public static TreeNode reConstructBinaryTree(int [] pre,int [] in) { /** * 在C/C++中可以用指针直接标识开始 和结束 *a a++ 就可以标识数组情况 但是对Java ,可以使用数组下标的方式标识起点个结束点 */ TreeNode root = constructBiTree(pre, 0, pre.length-1, in, 0, in.length-1); return root; } public static TreeNode constructBiTree(int[] pre ,int startPre, int endPre, int[] in ,int startIn, int endIn){ // System.out.println("startPre:"+startPre); if(startPre >endPre || startIn > endIn){ return null; } TreeNode root = new TreeNode(pre[startPre]); // i 指的是 in 数组下标 for(int i = startIn; i <= endIn;i++){ if(in[i] == pre[startPre]){ root.left = constructBiTree(pre, startPre+1,startPre+i-startIn, in,startIn,i-1); root.right = constructBiTree(pre,startPre+i-startIn+1, endPre,in , i+1, endIn); } } return root; } public static void PreOrder(TreeNode root){ if(root != null){ System.out.print(root.val+" "); PreOrder(root.left); PreOrder(root.right); } } public static void InOrder(TreeNode root){ if(root != null){ InOrder(root.left); System.out.print(root.val+" "); InOrder(root.right); } } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub /* int[] a ={1,2,3,4,5,6,7,8}; int[] b = new int[10]; System.arraycopy(a, 0, b, 0, 3); for(int i = 0; i < b.length ;i++) System.out.print(b[i]+" ");*/ int[] pre={1,2,4,7,3,5,6,8}; int[] in = {4,7,2,1,5,3,8,6}; TreeNode root = reConstructBinaryTree(pre, in); PreOrder(root); System.out.println(); InOrder(root); }}
重建二叉树:
#include<iostream>#include<stdio.h>#include<cstdlib>#include<cstring>using namespace std;//定义一颗二叉树// BiNode 可以看成二叉树一个节点 *BiTree定义了一个指针变量, 表示树typedef struct BiTNode{ int data; struct BiTNode *lchild, *rchild;}BiTNode, *BiTree;void createBiTree(BiTree T){ int ch ; cin>>ch; if(ch == 9999){ T = NULL; } else{ T = (BiTree)malloc(sizeof(BiTNode)); //申请内存失败 ,报错 if(!T){ cout<<"Get Space Error"<<endl; } (T)->data = ch; createBiTree(T->lchild); createBiTree(T->rchild); }}BiTree insert(BiTree root, int key ){ BiTree p = root; //新建一个节点 有一个父亲节点 BiTree ptr , pa ; ptr =(BiTree)malloc(sizeof(BiTNode)); if(ptr==NULL){ cout<<"申请空间失败"<<endl; } ptr->rchild = NULL; ptr->lchild = NULL; ptr->data = key; pa = NULL; if(p == NULL){ return ptr; }else{ while(p!= NULL){ // 标记当前到哪个点 pa = p; if(key <= p->data){ p = p->lchild; }else{ p = p->rchild; } } if(key <= pa->data){ pa->lchild = ptr; }else{ pa->rchild = ptr ; } } return root ;}BiTree createTree(){ BiTree root = NULL; int ch ; cin>>ch; while(ch != 9999){ root = insert(root, ch); cin>>ch; } return root;}void preOrder(BiTree root){ if(root != NULL){ cout<<root->data<<" "; preOrder(root->lchild); preOrder(root->rchild); }}void inOrder(BiTree root){ if(root!= NULL){ inOrder(root->lchild); cout<<root->data<<" "; inOrder(root->rchild); }}void postOrder(BiTree root){ if(root != NULL){ postOrder(root->lchild); postOrder(root->rchild); cout<<root->data<<" "; }}BiTree constructBiTree(int* startpre , int* endpre, int* startin, int* endin){ int pos; BiTree root; int rootValue = startpre[0]; cout<<"rootValue:"<<rootValue<<endl; root = (BiTree)malloc(sizeof(BiTNode)); root->lchild = NULL; root->rchild = NULL; root->data = rootValue; // 如果只有一个节点 if(startpre== endin){ if(startin == endin && *startin == *endin){ return root; }else{ cout<<"输入参数有误"<<endl; } } //遍历中序 找到先序节rootvalue 在中序中位置 int *rootIn = startin; while(rootIn <= endin && *rootIn!=rootValue){ rootIn++; } // 将先序数组分成两半 int leftLength = rootIn - startin; int *leftEndpre = startpre + leftLength; if(leftLength >0){ root->lchild = constructBiTree(startpre+1, leftEndpre,startin ,rootIn-1); } if(leftLength < endpre - startpre){ root->rchild = constructBiTree(leftEndpre+1, endpre, rootIn+1, endin); } return root;}BiTree rebuildree(int *pre , int *in ,int length){ if(pre == NULL || in == NULL || length <=0){ return NULL; } return constructBiTree(pre,pre+length-1, in, in+length-1);}int main(){ int pre[] ={1,2,4,7,3,5,6,8}; int in[] ={4,7,2,1,5,3,8,6}; BiTree root = rebuildree(pre, in,8);// BiTree root = createTree(); preOrder(root); cout<<endl; inOrder(root); cout<<endl; postOrder(root); return 0;}
阅读全文
0 0
- 剑指Offer-- 重建二叉树 前序中序建立二叉树
- [剑指offer]重建二叉树
- 【剑指offer】重建二叉树
- 剑指offer--重建二叉树
- 剑指offer---重建二叉树
- 剑指Offer-重建二叉树
- 剑指offer:重建二叉树
- 剑指offer-重建二叉树
- 《剑指offer》重建二叉树
- 【剑指Offer】重建二叉树
- 剑指offer-重建二叉树
- 《剑指offer》-重建二叉树
- 剑指offer 重建二叉树
- 剑指offer:重建二叉树
- 【剑指offer】重建二叉树
- 剑指offer 重建二叉树
- [剑指offer]重建二叉树
- 剑指offer--重建二叉树
- 关于java中下载
- CSS控制table中的文字垂直居中
- leetcode 第10题 Regular Expression Matching
- 构造器函数
- 百度地图,搜索功能,地图的高级引用
- 剑指Offer-- 重建二叉树 前序中序建立二叉树
- 对取模后的余数的理解
- Java中的XML文件写入
- 摄像头驱动(一)————V4L2框架浅析
- phpcms实现手机端与PC端双模板的方法与详细教程
- vim学习之(一)
- Spring中的ResourceLoaderAware小知识
- PHP 根据IP地址获取所在城市
- iBatis中RowHandler处理大数据量的导出