剑指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 镜像二叉树 ...
随机推荐
- AO中的GraphicsLayer---------元素的容器
come from AO中的GraphicsLayer---------元素的容器 图形元素(Graphic Element)是存储于GraphicsLayer中的,本文主要涉及的接口和类主要有以下几 ...
- ASP.NET MVC- 在Area里使用RedirectToAction跳转出错的解决方法
mvc使用Area分区开发后,存在不同Area之间的跳转,需要为每个区间添加Area规则,如下: 按 Ctrl+C 复制代码 using System.Web.Mvc; namespace web.A ...
- 在XAF(ASP.NET)中以ListEditor的形式调用百度地图API
因为项目需要,在系统中使用地图显示设备的地理位置.考虑过ArgGIS,Bing和Baidu地图.本来想用ArgGIS,看教程嫌麻烦.所以还是用Web地图吧.Bing的话还要申请个key,没心情.百度地 ...
- 跟SAP系统集成的Android应用
首先吐槽一点,这是我的第一个Android应用,很糙. 这个应用适合于上了SAP系统的企业内部使用,并且限于制造型MTO模式,需要针对生产订单报工操作的场景,因为此应用主要的一个目的,就是用来方便报工 ...
- new 运算符
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/new
- iOS开发-核心动画随笔
核心动画可以让View旋转,缩放,平移(主要是操作View的layer(层)属性)但是核心动画改变的位置不是真实的位置,一切都是假象所以有时候要用到其他动画,如UIView本来封装的动画,还有定时器 ...
- 安装Exchange2010
1.exadmin加入到 Schema admins,enterprise admins组中 CAS,HUB,MB安装.Net Framework CAS,HUB:2.Run 'ServerManag ...
- 【转】selenium及webdriver的原理
主要内容转自:http://blog.csdn.net/ant_ren/article/details/7968582和http://blog.csdn.net/ant_ren/article/det ...
- Android 中View的绘制机制源代码分析 三
到眼下为止,measure过程已经解说完了,今天開始我们就来学习layout过程.只是在学习layout过程之前.大家有没有发现我换了编辑器,哈哈.最终下定决心从Html编辑器切换为markdown编 ...
- little's law(律特法则)
参考:https://en.wikipedia.org/wiki/Little%27s_law(周末看一下) 最近在做性能压力测试,开始时,压力压不上去,参考: N = X * E[T] ,N就是你的 ...