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 ...
随机推荐
- (转)Shell脚本之break,continue,和exit区别
Linux脚本中的break continue exit return break结束并退出循环 continue在循环中不执行continue下面的代码,转而进入下一轮循环 exit退出脚本,常带一 ...
- eclipse启动的时候报错An internal error occurred during: "Initializing Java Tooling"
eclipse ->windows ->Perspactive -> Reset perspactive 重置视图可以解决
- ApplicationContextAware的作用
ApplicationContextAware其实我们看到---Aware就知道是干嘛用的了,就是属性注入的, 但是这个ApplicationContextAware的不同地方在于,实现了这个接口的b ...
- git 摘要
git使用摘记 git冲突的问题主要是在修改的部分而不是添加的部分, 如果merge的文件在同一个位置有不同的信息则git会报错 git push origin中的origin表示的是远程的仓库名为o ...
- GitKraken使用教程-基础部分(8)
9. 远程(Remote)仓库 1) 添加远程仓库 一般在本地新建仓库后,需要添加一个远程仓库用于push/pull代码.鼠标移至GitKraken左侧区域的REMOTE栏,点击 该栏右边出现的 按 ...
- schema中属性声明
<attribute name="属性名" default="默认值" fixed="固定值" use="option ...
- [LeetCode]17. Letter Combinations of a Phone Number电话号码的字母组合
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that th ...
- 栅格那点儿事(四D)
统计值与空值 在上一篇的内容里反复提到了一个统计值.那这个统计值是怎么来的,具体是干嘛用的呢? 统计值主要就是用于栅格数据的显示和重分类,顾名思义就是一个波段中所有像元值的一个统计信息,最大值,最小值 ...
- wechat开发笔记之1.线上环境搭建与测试
Wechat开发笔记 线上环境搭建: 申请一个wechat公众平台. 手机个人微信可以用webwechat来测试. Website:https://web.weixin.qq.com/ 手机客户端扫一 ...
- System Center Configuration Manager 2016 必要条件准备篇(Part4)
步骤4.重新启动Configuration Manager主服务器 注意:在Configuration Manager服务器(CM01)上以本地管理员身份执行以下操作 打开管理命令提示符并发出以下 ...