LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
要求:通过二叉树的前序和中序遍历序列构建一颗二叉树
代码如下:
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x): val(x),left(NULL), right(NULL) {}
};
typedef vector<int>::iterator Iter;
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder)
{
return buildTreeRecur(preorder.begin(), preorder.end(), inorder.begin(), inorder.end());
}
TreeNode *buildTreeRecur(Iter pstart, Iter pend, Iter istart, Iter iend)
{
if(pstart == pend || istart == iend)
return NULL;
int ival = *pstart;
Iter ipos = find(istart, iend, ival);
TreeNode *res = new TreeNode(ival);
res->left = buildTreeRecur(pstart + , pstart++(ipos-istart), istart, ipos);
res->right = buildTreeRecur(pstart++(ipos-istart), pend, ipos+, iend);
return res;
}
LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium的更多相关文章
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 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 t ...
- [Leetcode] Construct binary tree from preorder and inorder travesal 利用前序和中续遍历构造二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- 【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 105 Construct Binary Tree from Preorder and Inorder Traversal 从前序与中序遍历序列构造二叉树
给定一棵树的前序遍历与中序遍历,依据此构造二叉树.注意:你可以假设树中没有重复的元素.例如,给出前序遍历 = [3,9,20,15,7]中序遍历 = [9,3,15,20,7]返回如下的二叉树: ...
- [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: 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 @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根 ...
- 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 ...
随机推荐
- 通过windows远程桌面连接CentOS系统
前提: CentOS安装桌面,如果无桌面,请执行 # yum -y groups install "GNOME Desktop" # startx 1 2 配置源 # yum in ...
- springcloud-知识点总结(二):Ribbon&Feign
1.Ribbon简介 前面讲了eureka服务注册与发现,但是结合eureka集群的服务调用没讲. 这里的话 就要用到Ribbon,结合eureka,来实现服务的调用: Ribbon是Netflix发 ...
- jQuery截取字符串的几种方法
1.取后缀 var fileDir = $("#file").val(); var suffix = fileDir.substr(fileDir.lastIndexOf(&quo ...
- c#: Label控件加入AutoHeight属性
此功能在界面布局中颇为实用,录代码以记之: public class LabelEx : Label { private bool autoHeight = true; [DefaultValue(t ...
- 如何访问dashed filename
第一关:boJ9jbbUNNfktd78OOpsqOltutMc3MY1 第二关:CV1DtqXWVFXTvM2F0k09SHz0YwRINYA9 cat <- 第三关:如何访问spaces i ...
- Xadmin弹出窗口
Xadmin弹出窗口 需求分析: 1.在添加页面的一对多和多对多字段后面加上+,点击+后,能显示出添加相应字段的窗口 2.提交后窗口关闭,添加的内容显示到当前页面 1.判断出当前字段是否为Foreig ...
- C#,如何程序使用正则表达式如何使用匹配的位置的结果修改匹配到的值
程序代码使用正则表达式如何修改匹配到的值: 代码一: using System; using System.Text.RegularExpressions; public class Example ...
- 利用maven将项目打包成一个可以运行的独立jar包
目标:希望把Java项目打包成一个完整的jar包,可以独立运行,不需要再依赖其他jar包. 我们在用eclipse中mvn创建mvn项目的时候,选择非webapp,会默认的以jar打包形式,如下图: ...
- django by example 第四章 扩展 User 模型( model)
描述: RelatedObjectDoesNotExist at /account/edit/ User has no profile. 原因: 注意原书,要求新建一个账户才能使用.
- exception is java.lang.IllegalArgumentException: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make su re that file is correct.
spring cloud 项目使用maven 打包报错“No auto configuration classes found in META-INF/spring.factories” 在pom.x ...