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 ...
随机推荐
- 导入Android工程源码出现乱码问题的解决方法
可以尝试着从以下三个方法进行调试,一般情况下会完美解决的: 1.windows->Preferences->General->Content Types->Text->J ...
- 使用Thinkphp框架开发移动端接口
本文给大家分享的是使用thinkphp框架开发移动端接口的2种方法,一种是开发API,另外一种是实现移动端访问自动切换移动主题模板,从而实现伪app访问,下面我们就来详细看下如何实现吧. 方案一:给 ...
- Javascript跳转手机站代码
$(document).ready(function(){ var mobileAgent = new Array("iphone", "ipod", &quo ...
- shell条件测试
文件状态测试-b filename : 当filename 存在并且是块文件时返回真(返回0)-c filename : 当filename 存在并且是字符文件时返回真-d pathname : 当p ...
- UBUNTU下FPT工具--lftp使用说明
lftp 是UBUNTU下一个功能强大的下载工具,它支持访问文件的协议: ftp, ftps, http, https, hftp, fish.(其中ftps 和https需要在编译的时候包含open ...
- hdu 5128 The E-pang Palace
http://acm.hdu.edu.cn/showproblem.php?pid=5128 题意:给定N个点,选出其中8个点组成两个矩形,使得两个矩形的面积和最大. 思路:找出所有的矩形,然后枚举, ...
- win7计划任务执行BAT文件问题
今天下午做了一个调用java 可执行jar的程序,想通过win7的计划任务来调用 批处理命令: java -jar BIDropSyc.jar 或者 javaw -jar BIDropSyc.j ...
- Maven实战五
转载:http://www.iteye.com/topic/1123232 我们项目中用到的jar包可以通过依赖的方式引入,构建项目的时候从Maven仓库下载即可. 1. 依赖配置 依赖可以声明 ...
- 分页SQL模板
select * from ( select rownum as rn ,a.* from ( select * from page a where object_id >1000 and ow ...
- COJ 0970 WZJ的数据结构(负三十)树分治
WZJ的数据结构(负三十) 难度级别:D: 运行时间限制:1000ms: 运行空间限制:262144KB: 代码长度限制:2000000B 试题描述 给你一棵N个点的无根树,点和边上均有权值.请你设计 ...