[leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)
原题
题意:
根据先序和中序得到二叉树(假设无重复数字)
思路:
先手写一次转换过程,得到思路。
即从先序中遍历每个元素,(创建一个全局索引,指向当前遍历到的元素)在中序中找到该元素作为当前的root,以该节点左边所有元素作为当前root的左支,右同理。
重复分别对左右边所有元素做相同处理。
class Solution
{
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder)
{
int pos = 0;
return buildBinary(preorder, inorder, 0, preorder.size(), pos);
}
private:
TreeNode *buildBinary(vector<int> &preorder, vector<int> &inorder, int beg,
int end, int &pos)
{
TreeNode *node = NULL;
if (beg < end)
{
int i = 0;
for (i = beg; i < end; i++)
{
if (preorder[pos] == inorder[i])
break;
}
++pos;
node = new TreeNode(inorder[i]);
node->left = buildBinary(preorder, inorder, beg, i, pos);
node->right = buildBinary(preorder, inorder, i + 1, end, pos);
}
return node;
}
};
[leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal (Medium)的更多相关文章
- [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 ...
- (二叉树 递归) 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 ...
- 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 ...
- leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java
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 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- Java for 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 ...
- Leetcode#105 Construct Binary Tree from Preorder and Inorder Traversal
原题地址 基本二叉树操作. O[ ][ ] [ ]O[ ] 代码: TreeNode *restore(vector< ...
- leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal,剑指offer 6 重建二叉树
不用迭代器的代码 class Solution { public: TreeNode* reConstructBinaryTree(vector<int> pre,vector<in ...
- 【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 ...
随机推荐
- 解决C/C++程序执行一闪而过的方法(使用getchar,或者cin.get,不推荐system(“pause”))
简述 在VS编写控制台程序的时候,包括使用其他IDE(Visual C++)编写C/C++程序,经常会看到程序的执行结果一闪而过,要解决这个问题,可以在代码的最后加上system(“pause”).g ...
- 開發PlainTasks與JSON的插件
PlainTasks 是款很有名的任務管理插件,具體的介紹在這裡. 我最近的工作作務,是開發一款插件,能實現 JSON 文件到 todo 類文件的轉換. JSON 的格式是這樣的 1: { 2: &q ...
- c# Lambda扩展
扩展类 public static class LinqExtensions { /// <summary> /// 创建lambda表达式:p=>true /// </sum ...
- InnoExtractor可以解压一些安装文件,以获得其中的特殊文件
you can use InnoExtractor unpack the installer file and get uniSimpleEnc.dcu file. https://www.board ...
- java.lang.ClassNotFoundException: org.hibernate.ejb.HibernatePersistence 解决方法
编译遇到错误,如下: May 11, 2017 1:49:42 PM org.springframework.web.context.ContextLoader initWebApplicationC ...
- 简洁的描述SpringMVC工作流程
1.客户端发送来的请求 经过前端控制器(springDispatcherServlet)调用映射器(HandlerMapping)来找到对应的执行链(HandlerExecutionChain)对象, ...
- 10 关于DOM的操作
一.JavaScript的组成 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:文档对象 ...
- 一步一步教你用IntelliJ IDEA 搭建SSM框架(3)——实现用户登录功能
上面两篇博客已经详细的介绍了在IntelliJ IDEA 搭建SSM框架的整个过程,下面我们就要在搭建好的环境里实现我们想要的功能了.本文完成用户的登录功能,主要包括:用户注册,登录,编辑,退出,注销 ...
- Django 你需要掌握的模型层(标签、过滤器、模板的继承与导入)
Django 模型层(标签.过滤器.模板的继承与导入) 好文章来自超哥:https://www.cnblogs.com/guanchao/p/11006062.html 过滤器/自定义过滤器 模板 ...
- 求你了,再问你Java内存模型的时候别再给我讲堆栈方法区了…
GitHub 4.1k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 4.1k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 4.1k Star 的 ...