leetcode第一刷_Construct Binary Tree from Preorder and Inorder Traversal
构造方式跟中序与后序全然一样,并且一般都习惯正着来,所以更简单。
代码是之前写的,没实用库函数,不应该。
TreeNode *buildIt(vector<int> &preorder, int start1, vector<int> &inorder, int start2, int len){
if(len <= 0)
return NULL;
TreeNode *root = new TreeNode(preorder[start1]);
int i = start2;
for(;i<start2+len&&inorder[i] != preorder[start1];i++);
int len2 = i-start2;
root->left = buildIt(preorder, start1+1, inorder, start2, len2);
root->right = buildIt(preorder, start1+1+len2, inorder, i+1, len-len2-1);
return root;
}
class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
if(preorder.size()<=0 || inorder.size()<=0)
return NULL;
return buildIt(preorder, 0, inorder, 0, preorder.size());
}
};
leetcode第一刷_Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 【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】#105. Construct Binary Tree from Preorder and Inorder Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源:http ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- LeetCode OJ 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 ...
- LeetCode OJ: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 OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
- leetcode第一刷_Construct Binary Tree from Inorder and Postorder Traversal
这道题是为数不多的感觉在读本科的时候见过的问题. 人工构造的过程是如何呢.兴许遍历最后一个节点一定是整棵树的根节点.从中序遍历中查找到这个元素,就能够把树分为两颗子树,这个元素左側的递归构造左子树,右 ...
- 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 ...
- LeetCode: Construct Binary Tree from Preorder and Inorder Traversal 解题报告
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
随机推荐
- 《find技巧》-“linux命令五分系列”之一
一天一个命令,做个记录, 我要成大神,哈哈哈 本原创文章属于<Linux大棚>博客. 博客地址为http://roclinux.cn. 文章作者为roc 希望您能通过捐款的方式支持Linu ...
- js常用效果
//创建元素 var txt1="<p style='color:red'>我是由HTML创建的</p>"; // 以 HTML 创建新元素 var txt ...
- Flask 富文本编辑器
XHEditor http://segmentfault.com/blog/digwtx/1190000002439076 CKeditor http://segmentfault.com/blog/ ...
- iOS9 以上的真机调试 不用证书
具体流程如下: 首次使用AppleID 的注意事项: 要在设置中 进行 如下操作 设置--通用--描述文件 ---添加信任 但是有时候 还是 会不能调试, 显示信息 是这样的 : ...
- GNU MAKE指南
GNU make 指南 翻译: 哈少 译者按: 本文是一篇介绍 GNU Make 的文章,读完后读者应该基本掌握了 make 的用法.而 make 是所有想在 Unix (当然也包括 Linux )系 ...
- 定义任务打印gradle下载的jar包位置
//定义任务打印gradle下载的jar包位置task showMyCache { configurations.compile.each { println it }}
- Find The Multiple(poj 1426)
Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose ...
- CP30,DBCP数据源配置
Spring中 CP30数据源配置 <!-- 加载属性文件 01--> <bean id= "propertyConfigurer" class="or ...
- mysql数据库操作语句大全
一 . 常用mysql命令行命令 1 .启动MYSQL服务 net start mysql 停止MYSQL服务 net stop mysql 2 . netstat –na | findstr 3 ...
- 【转】如何开发苹果iOS操作平台下的应用程序?
原文网址:http://zhidao.baidu.com/link?url=vxRWjCchSstFmVKvxEqLqfqomu2h5kF-NLAIVEehQgN_FnYtEi4f5yPMS6ywbU ...