Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal前序与中序构造二叉树
根据一棵树的前序遍历与中序遍历构造二叉树。
注意:
你可以假设树中没有重复的元素。
例如,给出
前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3,15,20,7]
返回如下的二叉树:
3 / \ 9 20 / \ 15 7
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder)
{
if(preorder.size() == 0)
return NULL;
if(preorder.size() == 1)
return new TreeNode(preorder[0]);
int iroot = preorder[0];
int ipos = 0;
for(int i = 0; i < inorder.size(); i++)
{
if(inorder[i] == iroot)
{
ipos = i;
break;
}
}
TreeNode *root = new TreeNode(iroot);
vector<int> v1(preorder.begin() + 1, preorder.begin() + ipos + 1);
vector<int> v2(inorder.begin(), inorder.begin() + ipos);
vector<int> v3(preorder.begin() + ipos + 1, preorder.end());
vector<int> v4(inorder.begin() + ipos + 1, inorder.end());
root ->left = buildTree(v1, v2);
root ->right = buildTree(v3, v4);
return root;
}
};
Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal前序与中序构造二叉树的更多相关文章
- LeetCode105. Construct Binary Tree from Preorder and Inorder Traversal
题目 根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3 ...
- 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 ...
- 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 ...
- C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告
剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...
- 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 ...
随机推荐
- spark 应用场景1-求年龄平均值
原文引自:http://blog.csdn.net/fengzhimohan/article/details/78535143 该案例中,我们将假设我们需要统计一个 10 万人口的所有人的平均年龄,当 ...
- SF Symbols 使用
伴随着WWDC 2019 的举办,对于程序员而言 ,无疑SwiftUI 推出 是比较令人兴奋的一件事情, 其中在SwiftUI 使用之中, 我们经常使用以下系统图片 Image(systemName: ...
- 编译安装redis-3.2.9(latest stable version)
What is the Redis? Redis is an open source (BSD licensed), in-memory data structure store, used as a ...
- Jmeter性能测试 入门【转】
Jmeter性能测试 入门[转] Jmeter是一款优秀的开源测试工具, 是每个资深测试工程师,必须掌握的测试工具,熟练使用Jmeter能大大提高工作效率. 熟练使用Jmeter后, 能用Jmeter ...
- 安卓手机端微信网页浏览记录清理debugx5.qq.com
最近我们环境从复)星(云切换到阿里云.早上地铁路上就有小伙伴@,一阵搜索.找的如下的方法. 记录一下: 目前只支持安卓手机的微信内置浏览器清理. 由腾讯提供的网址http://debugx5.qq.c ...
- js 验证图片
var selectedImg = e.target.files[0]; //获取图片 var isPic = /^(image\/bmp|image\/gif|image\/jpeg|image\/ ...
- http response 过长 导致Connection reset
http response 过长(2W byte) 导致Connection reset
- 杂项-DTO:DTO(数据传输对象)
ylbtech-杂项-DTO:DTO(数据传输对象) 数据传输对象(DTO)(Data Transfer Object),是一种设计模式之间传输数据的软件应用系统.数据传输目标往往是数据访问对象从数据 ...
- PHP面向对象访问修饰符的基本了解
l 文档的介绍: 对属性或方法的访问控制,是通过在前面添加关键字 public(公有),protected(受保护)或 private(私有)来实现的.被定义为公有的类成员可以在任何地方被访问.被定义 ...
- HttpServletRequest request 获取当前登录的用户-获取当前用户
有的业务需要知道当前登录的用户 当然需要引用这个啦 import javax.servlet.http.HttpServletRequest; 然后 HttpSession session = req ...