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 duplicates do not exist in the tree.
分析
给定一颗二叉树的前序和中序遍历序列,求该二叉树。
我们手动做过很多这样的题目,掌握了其规则~
前序遍历第一个元素为树的root节点,然后在中序序列中查找该值,元素左侧为左子树,右侧为右子树; 求出左子树个数count,在前序序列中 , 除去第一个节点,接下来的count个元素构成左子树的前序序列,其余的构成右子树的前序序列。
开始,没有采用迭代器,声明vector占用了大量空间,Memory Limit Exceeded。。。
代码为:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (preorder.empty() && inorder.empty())
return NULL;
//求树中节点个数
int size = preorder.size();
//先序遍历第一个节点为树的根节点
TreeNode *root = new TreeNode(preorder[0]);
int pos = 0;
//在中序遍历结果中查找根节点
for (int i=0; i<size; ++i)
{
if (inorder[i] == preorder[0])
{
pos = i;
break;
}//if
}//for
if (pos >= 0 && pos < size)
{
//则在inOrder中(0 , pos-1)为左子树中序遍历结果(pos+1,size-1)为右子树的中序遍历序列
//在preOrder中(1,pos)为左子树前序遍历结果(pos+1,size-1)为右子树前序遍历结果
vector<int> left_pre;
for (int j = 1; j <= pos; j++)
left_pre.push_back(preorder[j]);
vector<int> left_in;
for (int j = 0; j < pos; ++j)
left_in.push_back(inorder[j]);
root->left = buildTree(left_pre, left_in);
//构造右子树
vector<int> right_pre , right_in;
for (int j = pos + 1; j < size; j++)
{
right_pre.push_back(preorder[j]);
right_in.push_back(inorder[j]);
}
root->right = buildTree(right_pre, right_in);
}
return root;
}
};
然后,使用迭代器避免不必要的空间占用,AC~
AC代码
class Solution {
public:
template <typename Iter>
TreeNode* make(Iter pre_begin, Iter pre_end, Iter in_begin, Iter in_end) {
if (pre_begin == pre_end || in_begin == in_end)
return NULL;
//先序遍历第一个节点为树的根节点
TreeNode *root = new TreeNode(*pre_begin);
//在中序遍历结果中查找根节点
Iter iter = find(in_begin, in_end, *pre_begin);
int count = iter - in_begin;
if (iter != in_end)
{
//则在inOrder中(0 , pos-1)为左子树中序遍历结果(pos+1,size-1)为右子树的中序遍历序列
//在preOrder中(1,pos)为左子树前序遍历结果(pos+1,size-1)为右子树前序遍历结果
root->left = make(pre_begin + 1, pre_begin + count + 1, in_begin, iter);
//构造右子树
root->right = make(pre_begin + count + 1, pre_end, iter + 1, in_end);
}
return root;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (preorder.empty() || inorder.empty())
return NULL;
return make(preorder.begin(), preorder.end(), inorder.begin(), inorder.end());
}
};
LeetCode(105) Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal
Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or 'Inorder and ...
- 105 + 106. Construct Binary Tree from Preorder and Inorder Traversal (building trees)
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
Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a ...
- 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 ...
- LeetCode: 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 ...
- 【题解二连发】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 ...
- 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 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
随机推荐
- TensorFlow 模型保存/载入
我们在上线使用一个算法模型的时候,首先必须将已经训练好的模型保存下来.tensorflow保存模型的方式与sklearn不太一样,sklearn很直接,一个sklearn.externals.jobl ...
- Hive_Hive的管理_远程服务
远程服务启动方式 - 端口号10000 - 启动方式: #hive --service hiveserver & 以JDBC或ODBC的程序登陆到hive中操作数据时,必须选用远程服务启动方式 ...
- @Results( 中 params 怎么用
http://blog.csdn.net/z69183787/article/details/16342553 struts2的@Result annotation 如何添加params,并且在页面取 ...
- python入门之迭代器
迭代器 已知,可以直接作用于for循环的数据类型有: 一类是集合数据类型,如list.tuple.dict.set.str 一类是generator,包括生成器和带yield的generator fu ...
- mysql 维护添加远程主机访问
https://www.cnblogs.com/JNUX/p/6936548.html
- 使用nvmw解决windows下多版本node共存的问题
不支持4.x的nodejs,用nodist吧 不支持4.x的nodejs,用nodist吧 不支持4.x的nodejs,用nodist吧 ===========不要再看的分割线============ ...
- ZooKeeper理论知识
前言 相信大家对 ZooKeeper 应该不算陌生.但是你真的了解 ZooKeeper 是个什么东西吗?如果别人/面试官让你给他讲讲 ZooKeeper 是个什么东西,你能回答到什么地步呢? 我本人曾 ...
- Ice-cream Tycoon9(线段树)
线段树的一些基本应用,就是函数写了很多,有点繁琐. 以每个物品的单价建树,刚开始写了个裸的想水过去直接MLE了,然后又离散化了下. 离散化单价后建树,lz数组用来清零,s数组保存结点所含物品个数,co ...
- c#操作ecxel的一些资源(downmoon搜集)
c#操作ecxel的一些资源(downmoon搜集) 工作需要,邀月收集了几个操作excel的资源. 1.如何:使用 COM Interop 创建 Excel 电子表格(C# 编程指南)http:/ ...
- 4G牌照影响
与3G牌照发放整整讨论了10年不同,4G牌照发放在2009年3G规模建设4年后就进行了发放,也颇匹配于行业的加速度.那么,4G到底会在哪些方面.在何种程度上改变中国呢?其实,4G的影响可能没有那么大, ...