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
Tag
代码
0x0017F7D7 处有未经处理的异常(在 test.exe 中): 0xC00000FD: Stack overflow (参数: 0x00000001, 0x01242FBC)。
我的方法对于大数据的问题会栈溢出。
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (preorder.empty() || inorder.empty())
return nullptr;
//root
TreeNode* root = new TreeNode(preorder[0]);
TreeNode* cur = root;
preorder.erase(preorder.begin());
auto it = inorder.begin();
for (; it != inorder.end(); it++)
{
if (root->val == *it)
{
break;
}
}//在inorder中找到根节点的迭代器
vector<int> leftin;
vector<int> rightin;
for (auto item = inorder.begin(); item<it; item++)
{
leftin.push_back(*item);
}
inorder.erase(inorder.begin(),it+1);
rightin = inorder;
root->left = buildTree(preorder, leftin);
root->right = buildTree(preorder, rightin);
return root;
}
};
问题
LeetCode105. Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告
剑指offer 重建二叉树 提交网址: http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tq ...
- Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal前序与中序构造二叉树
根据一棵树的前序遍历与中序遍历构造二叉树. 注意: 你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [3,9,20,15,7] 中序遍历 inorder = [9,3,15 ...
- 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 ...
- 【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] 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 ...
随机推荐
- [转]Jquery Mobile dialog的生命周期
本文转自:http://www.cnblogs.com/jackhuclan/archive/2012/04/05/2432972.html JQuery Mobile对htm5的移动开发绝对是个好用 ...
- [转]jQuery Mobile动态刷新页面样式
本文转自:http://blog.csdn.net/zht666/article/details/8560765 当我们使用Ajax或者javascript动态在页面上添加元素后,如添加select控 ...
- WinPcap抓取数据包
#WinPcap和Libpcap的最强大的特性之一,是拥有过滤数据包的引擎. 它提供了有效的方法去获取网络中的某些数据包,这也是WinPcap捕获机制中的一个组成部分. 用来过滤数据包的函数是#
- DBCP数据库连接池原理分析
在比较大的项目中,需要不断的从数据库中获取数据,Java中则使用JDBC连接数据库,但是获取数据库的连接可是相当耗时的操作,每次连接数据库都获得 .销毁数据库连接,将是很大的一个开销.为了解决这种开销 ...
- FragmentPagerAdapter 与 FragmentStatePagerAdapter的区别。通过验证证明的。
在一个 Android 应用中,我使用 FragmentPagerAdapter 来处理多 Fragment 页面的横向滑动.不过我碰到了一个问题,即当 Fragment 对应的数据集发生改变时,我希 ...
- Quick-Cocos2d-x Lua脚本加密打包器
准备开新项目了,在寻找合适的框架,后来就发现了Quick-Cocos2d-x这玩意. 别说,还挺好使.之后一步步研究,发现Lua不加密是不行的. 加密的方法在这里. 因为在做版本更新的时候,一般大家都 ...
- python网络编程-paramiko模块
paramiko模块 该模块基于SSH用于连接远程服务器并执行相关操作 参考文档 SSHClient 用于连接远程服务器并执行命令 import paramiko #创建SSH对象 ssh = par ...
- Myeclipse与tomcat的运行问题
在myeclipse中修改自己servlet后,在次运行时,可能会没有变化,这时需要重启tomcat,重新加载servlet
- iOS获取/删除url中的参数
1.获取URL中的某个参数: - (NSString *)getParameter:(NSString *)parameter urlStr:(NSString *)url { NSError *er ...
- graphql 后台服务项目架构(一)
基础知识 简而言之,GraphQL 是一种描述如何请求数据的语法,通常用于客户端向服务器请求数据.GraphQL 有三个主要特点: 允许客户端精确指定所需数据. 可以更容易地从多个数据源聚合数据. 使 ...