代码有问题

 /*************************************************************************
> File Name: 04_Rebuild_BinaryTree.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: 2016年08月29日 星期一 16时32分35秒
************************************************************************/ #include <stdio.h>
#include <malloc.h> // 二叉树结构体
struct BinaryTree
{
int val;
struct BinaryTree* left;
struct BinaryTree* right;
}; void preorderPrintBinaryTree(struct BinaryTree* root)
{
if (root == NULL)
return;
printf("%d ", root->val);
preorderPrintBinaryTree(root->left);
preorderPrintBinaryTree(root->right);
} void inorderPrintBinaryTree(struct BinaryTree* root)
{
if (root == NULL)
return;
preorderPrintBinaryTree(root->left);
printf("%d ", root->val);
preorderPrintBinaryTree(root->right);
} void postorderPrintBinaryTree(struct BinaryTree* root)
{
if (root == NULL)
return;
preorderPrintBinaryTree(root->left);
preorderPrintBinaryTree(root->right);
printf("%d ", root->val);
} struct BinaryTree* rebuild_BinaryTree(int* startPreorder, int* endPreorder, int* startInorder, int* endInorder)
{
// 前序遍历第一个数字是根节点的值
int rootValue = startPreorder[];
struct BinaryTree* root = (BinaryTree*)malloc(sizeof(BinaryTree));
root->val = rootValue;
root->left = NULL;
root->right = NULL; if (startPreorder == endPreorder)
{
if (startInorder==endInorder && *startPreorder==*startInorder)
return root;
} // 在前序遍历中找根节点的值
int* rootInorder = startInorder;
while (rootInorder<=endInorder && *rootInorder!=rootValue)
++ rootInorder; int leftLength = rootInorder - startInorder;
int* leftPreorderEnd = startPreorder + leftLength;
if (leftLength > )
{
// 构建左子树
root->left = rebuild_BinaryTree(startPreorder+, leftPreorderEnd, startInorder, rootInorder-);
}
if (leftLength < endPreorder - startPreorder)
{
// 构建右子树
root->right = rebuild_BinaryTree(leftPreorderEnd+, endPreorder, rootInorder+, endInorder);
}
return root;
} struct BinaryTree* rebuild(int* preorder, int* inorder, int length)
{
if (preorder==NULL || inorder==NULL || length<=)
return NULL; return rebuild_BinaryTree(preorder, preorder+length-, inorder, inorder+length-);
} // ====================测试代码====================
void Test(int* preorder, int* inorder, int length)
{
printf("The preorder sequence is: ");
for(int i = ; i < length; ++ i)
printf("%d ", preorder[i]);
printf("\n"); printf("The inorder sequence is: ");
for(int i = ; i < length; ++ i)
printf("%d ", inorder[i]);
printf("\n"); struct BinaryTree* root = rebuild(preorder, inorder, length); printf("After rebuild:\n");
printf("\nThe preorder sequence is: ");
preorderPrintBinaryTree(root);
printf("\nThe inorder sequence is: ");
inorderPrintBinaryTree(root);
printf("\nThe postorder sequence is: ");
postorderPrintBinaryTree(root);
} int main()
{
int length = ;
int preorder[] = {, , , , , , , };
int inorder[] = {, , , , , , , }; Test(preorder, inorder, length);
}

剑指Offer04 重建二叉树的更多相关文章

  1. 剑指Offer-4.重建二叉树(C++/Java)

    题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2 ...

  2. 牛客_剑指offer_重建二叉树,再后续遍历_递归思想_分两端

       总结:    重建二叉树:其实就是根据前序和中序重建得到二叉树,得到后续,只要输出那边设置输出顺序即可 [编程题]重建二叉树 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的 ...

  3. 剑指offer--4.重建二叉树

    题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...

  4. 剑指Offer——重建二叉树

    题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7 ...

  5. 剑指Offer——重建二叉树2

    Question 输入某二叉树的后序遍历和中序遍历的结果,请重建出该二叉树.假设输入的后序遍历和中序遍历的结果中都不含重复的数字.例如输入后序遍历序列{1, 3, 4, 2}和中序遍历序列{1, 2, ...

  6. 剑指offer--19.重建二叉树

    先序:根>左>右 中序:左>根>右 后序:左>右>根 e.g. {1,2,4,7,3,5,6,8} {4,7,2,1,5,3,8,6} 先序第一个元素是根节点,在中 ...

  7. 用js刷剑指offer(重建二叉树)

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  8. 剑指offer 重建二叉树

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...

  9. 《剑指offer》 二叉树的镜像

    本题来自<剑指offer>二叉树的镜像 题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 ...

随机推荐

  1. myeclipse 8.5安装freemarker插件方法

    1. 下载freemarker最新版本,目前本人下载时最新版本是:freemarker-ide-0.9.14.zip, ide版本的freemarker.jar版本太低,可以替换成高级版本,并修改me ...

  2. EasyUI Accordion下的Panel面板初始化时全部折叠

    EasyUI Accordion下的Panel面板有一个属性:selected,默认值为:false.初始化时,若设置'selected:true',则面板默认打开,效果如下: <div tit ...

  3. 最小生成树之Prime法

    关于最小生成树的概念,在前一篇文章中已经讲到,就不在赘述了.下面介绍Prime算法:         其基本思想为:从一个顶点出发,选择由该顶点出发的最小权值边,并将该边的另一个顶点包含进来,然后找出 ...

  4. 4.接口隔离原则(Interface Segregation Principle)

    1.定义 客户端不应该依赖它不需要的接口: 一个类对另一个类的依赖应该建立在最小的接口上. 2.定义解读 定义包含三层含义: 一个类对另一个类的依赖应该建立在最小的接口上: 一个接口代表一个角色,不应 ...

  5. 理解Web标准(网站标准)

    我觉得一名Web前端应该好好理解Web标准到底是什么,为什么要在我们的实际实践中遵循Web标准. 什么是Web标准.百度百科的解释是: WEB标准不是某一个标准,而是一系列标准的集合.网页主要由三部分 ...

  6. 整合spring roo,maven,mybatis,spring-flex,blazeds,mysql

    1.      下载spring roo,设置环境变量ROO_HOME,和path,classpath. 使用CMD命令行找到工作区间,新建工程目录转到工程目录:mkdir ten-minutes $ ...

  7. 让<a></a>diabled 的方法

    <a href="test_att_data.php" disabled="true" onclick="return false"& ...

  8. unity3d脚本编程

    一 创建和使用脚本 1 概述 GameObject的行为都是被附加到其上面的组件控制,脚本本质上也是一个组件. 在unity中创建一个脚本,默认内容例如以下: using UnityEngine; u ...

  9. Android开发代码混淆经验(Eclipse)

    为了防止自己的劳动成果被别人窃取,混淆代码能有效防止被反编译,下面来总结以下混淆代码的步骤: 2.编辑项目下的proguard-project.txt,添加不需要混淆的规则(model.泛型.反射.第 ...

  10. 【JSP】JSTL使用core标签总结(不断更新中)

    使用core标签 在页面中使用taglib指令指定标签URI和prefix.如: <%@ taglib uri="http://java.sun.com/jsp/jstl/core&q ...