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 ...
随机推荐
- 编译opencv python接口
首先,安装依赖1 sudo apt-get install build-essential 2 sudo apt-get install cmake git libgtk2.0-dev pkg-con ...
- jdk 动态代理 数据连接池
package com.itheima.datasource; import java.io.PrintWriter; import java.lang.reflect.InvocationHandl ...
- mongodb3集群搭建
三台服务器:先设置hosts 10.0.0.231 node1 10.0.0.232 node2 10.0.0.233 node3 1:下载 mongodb-linux-x86_64-rhel70-3 ...
- contiki源码阅读之list
我们阅读一下contiki的源码,list.c(路径是./core/lib/list.h). #include "lib/list.h" #define NULL 0 struct ...
- Spring Boot(一)Hello World
Spring Boot适合与微服务,方便快速开发,简化配置(约定>配置). 准备工作: SpringBoot 2.0 jdk8 IDEA或者eclipse+Spring Tool Suits 创 ...
- c++关于map的find和count的使用
编程的时候比较常用,今天记录一下,以后备用. 使用count,返回的是被查找元素的个数.如果有,返回1:否则,返回0.注意,map中不存在相同元素,所以返回值只能是1或0. 使用find,返回的是被查 ...
- HDU 3829——Cat VS Dog——————【最大独立集】
Cat VS Dog Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit S ...
- C#中实现输入汉字获取其拼音(汉字转拼音)的2种方法
主要介绍了C#中实现输入汉字获取其拼音(汉字转拼音)的2种方法,本文分别给出了使用微软语言包.手动编码实现两种实现方式,需要的朋友可以参考下 本文刚发布时,只写了一个实现方式,使用的是微软的语言包,但 ...
- 关于web.xml的welcome-file-list 配置与tomcat的关系:
关于web.xml的welcome-file-list 配置与tomcat的关系: 2018年04月18日 10:17:13 守望dfdfdf 阅读数:377 标签: welcome-file-lis ...
- ArcGIS API for JavaScript开发初探——基本地图组件使用
1.前言 在上一篇我们已经我们已经讲述了第一个地图应用程序的HelloMap的创建过程,这一篇我们来讲述基本地图组件:Home Button.比例尺.鹰眼图的使用方法. 2.基本地图组件 在ArcGI ...