72_leetcode_Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
1:注意特殊情况。2:递归的情况;3:递归结束情况;4:首先获得根节点,之后把两个数组分别分成两部分,递归分别得出左子树和右子树。
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder)
{
if(preorder.size() == 0 || inorder.size() == 0 || preorder.size() != inorder.size())
{
return NULL;
} int size = (int)preorder.size(); return buildTreeCore(preorder, 0, inorder, 0, size);
} TreeNode* buildTreeCore(vector<int> &preorder, int preStart, vector<int> &inorder, int preIn, int length)
{
if(length == 1)
{
if(preorder[preStart] != inorder[preIn])
{
return NULL;
}
} int rootValue = preorder[preStart];
TreeNode *root = new TreeNode(rootValue); int i = 0; for(; i < length; i++)
{
if(inorder[preIn + i] == rootValue)
{
break;
}
} if(i == length)
{
return NULL;
} if(i > 0)
{
root->left = buildTreeCore(preorder, preStart + 1, inorder, preIn, i);
} if(i < length - 1)
{
root->right = buildTreeCore(preorder, preStart + i + 1, inorder, preIn + i + 1, length - 1 - i);
} return root;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
72_leetcode_Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 36. Construct Binary Tree from Inorder and Postorder Traversal && Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/cons ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- 【题解二连发】Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree from Preorder and Inorder Traversal
LeetCode 原题链接 Construct Binary Tree from Inorder and Postorder Traversal - LeetCode Construct Binary ...
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- LeetCode_Construct Binary Tree from Preorder and Inorder Traversal
一.题目 Construct Binary Tree from Preorder and Inorder Traversal My Submissions Given preorder and ino ...
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- 使用git微命令深入理解git工作机制
首先.这篇不是真正意义上的翻译,所以大家在看的时候不要找相应的英文文章相应着看.这篇文章之所以归类为翻译.是由于最開始有一篇英文文章让我对git内部机制有了清楚的认识,它能够说是我git的启蒙老师吧. ...
- UPX 加壳工具:The Ultimate Packer for eXecutables
UPX (the Ultimate Packer for eXecutables)是一款先进的可运行程序文件压缩器.压缩过的可运行文件体积缩小50%-70% ,这样降低了磁盘占用空间.网络上传下载的时 ...
- spring-framework-3.2.4.RELEASE 综合hibernate-release-4.3.5.Final一个错误Caused by: java.lang.NoClassDefFound
LZ一体化的今天spring-framework-3.2.4.RELEASE 综合hibernate-release-4.3.5.Final一个错误Caused by: java.lang.NoCla ...
- Lu核心库系统结构及输出函数
Lu核心库系统结构及输出函数 Lu来源于Forcal,可以说,没有Forcal就没有Lu,但学习Lu并不需要了解Forcal. Lu是对Forcal的完善和发展,但与Forcal相比,Lu更简洁实用. ...
- android一个上传图片的样例,包含怎样终止上传过程,假设在上传的时候更新进度条(一)
先上效果图: Layout为: <? xml version="1.0" encoding="utf-8"?> <LinearLayout x ...
- RGB转为Lab空间
虽然若干年前就看过了关于色彩空间的介绍,但是直到今天才自己动手写代码做这件事情.虽然网络上已经有很多现成的例子,但是一则仅仅适用于浮点型的数据,另一方面,在实现上也有一些尚可优化之处. 色彩模型除了最 ...
- OCA读书笔记(15) - 执行数据库备份
物理备份 -- 数据文件,控制文件,日志文件,参数文件 数据库备份 冷备 -- 归档和非归档均可以 什么时候必须用冷备?1. 数据库的模式为非归档的2. 用于现场保护 冷备的过程:1. 首先查看备份文 ...
- CString的部分实现剖析
一.CString初探: 在CString的实现中,其最基础的类结构如下: CString其实只有一个数据成员m_pszData,这个成员指向了字符串的首地址.但在MFC的具体实现中, m_pszDa ...
- 查看mysql数据库表大小和最后修改时间
查看mysql数据库表相关信息如表大小.修改更新等信息,可以通过以下方式: 一 show table status like ’table_name‘ ; 二 在infortmation_sche ...
- hdu4521(线段树+dp)
传送门:小明系列问题——小明序列 题意:有n个数,求间距大于d的最长上升序列. 分析:dp[i]表示在i点以a[i]结束距离大于d的最长上升序列,然后每更新到第i点时,取i-d之前小于a[i]的数为结 ...