剑指Offer04 重建二叉树
代码有问题
/*************************************************************************
> 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 重建二叉树的更多相关文章
- 剑指Offer-4.重建二叉树(C++/Java)
题目: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2 ...
- 牛客_剑指offer_重建二叉树,再后续遍历_递归思想_分两端
总结: 重建二叉树:其实就是根据前序和中序重建得到二叉树,得到后续,只要输出那边设置输出顺序即可 [编程题]重建二叉树 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的 ...
- 剑指offer--4.重建二叉树
题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2, ...
- 剑指Offer——重建二叉树
题目描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7 ...
- 剑指Offer——重建二叉树2
Question 输入某二叉树的后序遍历和中序遍历的结果,请重建出该二叉树.假设输入的后序遍历和中序遍历的结果中都不含重复的数字.例如输入后序遍历序列{1, 3, 4, 2}和中序遍历序列{1, 2, ...
- 剑指offer--19.重建二叉树
先序:根>左>右 中序:左>根>右 后序:左>右>根 e.g. {1,2,4,7,3,5,6,8} {4,7,2,1,5,3,8,6} 先序第一个元素是根节点,在中 ...
- 用js刷剑指offer(重建二叉树)
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- 剑指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 ...
- 《剑指offer》 二叉树的镜像
本题来自<剑指offer>二叉树的镜像 题目: 操作给定的二叉树,将其变换为源二叉树的镜像. 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 ...
随机推荐
- JAVA自定义注释(Target,Retention,Documented,Inherit)
java自定义注解 Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明.配置的功能.注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用.包含在 java.l ...
- C#核心基础--类(2)
C#核心基础--类的声明 类是使用关键字 class 声明的,如下面的示例所示: 访问修饰符 class 类名 { //类成员: // Methods, properties, fields, eve ...
- IE6常见bug整理
By Diaoyude | 发布时间: 09-08 09:47 | Hits:1,253 | Post in: WEB前端 , Div-Css 针对IE6常见的一些ie6bug,ie6png,E6 ...
- ASP.NET基础之HttpModule 、HttpContext、 HttpHandler
http://www.cnblogs.com/wujy/p/3261141.html http://www.cnblogs.com/wujy/p/3264475.html http://www.cnb ...
- 检测一个DOM对象是否为空
我们时常要检测一个DOM对象是否为空. var $jObject = $('#btn'); alert($jObject ); 我们会发现,$jObject 永远不会为空.为什么呢?$ 方法查找对象, ...
- Java Zip压缩实现
最近在自学javaWeb,先复习一下java,把还给老师的东西再找回来(知识如果不用很快就会忘记啊).. 今天看到了zip压缩,决定要整理一下. java将有关zip压缩的内容都封装在java.uti ...
- Windows创建自动化任务
Windows创建自动化任务使得开机就打开相应的Python目录 1:计算机管理 2:找到任务计划程序 3:创建基本任务 4:任务触发器 5: 建立bat执行文件 start "" ...
- 使用Url.Routeurl获取url值。
1,获取url值. public ActionResult About() { RouteValueDictionary RVD = new Ro ...
- [Express] Level 5: Route file
Using a Router Instance Let's refactor app.js to use a Router object. Create a new router object and ...
- 怎样通过Java程序提交yarn的mapreduce计算任务
因为项目需求,须要通过Java程序提交Yarn的MapReduce的计算任务.与一般的通过Jar包提交MapReduce任务不同,通过程序提交MapReduce任务须要有点小变动.详见下面代码. 下面 ...