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 ...
随机推荐
- python中的os.path.dirname与os.path.dirname(__file__)的用法
python中的os.path.dirname的用法 os.path.dirname(path) 语法:os.path.dirname(path) 功能:去掉文件名,返回目录 如: print(os. ...
- 543 Diameter of Binary Tree 二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树 1 / \ 2 ...
- 使用express+mongoDB搭建多人博客 学习(1) 安装blog工程
一.安装 1.安装express npm install -g expressnpm install -g express-generator 2.用ejs做模板,新建blog工程express -e ...
- js去掉字符串前后以及中间的空格
p['bank_card'] = p['bank_card'].replace(/(^\s+)|(\s+$)/g,"");p['bank_card'] = p['bank_card ...
- 一步步实现自己的ORM(四)
通过前3章文章,大致对ORM有一定的了解,但也存在效率低下(大量用了反射)和重复代码,今天我们要对ORM进行优化. 具体流程如下: 我们优化的第一个就是减少反射调用,我的思路是定义一个Mapping, ...
- 终端工具Xmanager使用技巧
1. 新建绘画使用终端连接服务器 2. 设置终端类型和编码 3. 设置终端外观,包括字体颜色等等 4. 设置默认上传路径和下载路径
- scrapy安装遇到的Twisted问题
贴上大佬的博客地址:https://blog.csdn.net/a19990412/article/details/78849881 电脑一直在爆下面这一堆的信息 Command”c:\users\l ...
- JQ中的问题
$(function(){$(document).bind("click", function (e) {$(e.target).closest("p").cs ...
- WebService学习之旅(六)使用Apache Axis2实现WebService客户端调用
上节介绍了如何使用Axis2 发布一个WebService,Axis2除了为我们编写WebService应用带来了便利,也同样简化的客户端调用的过程,本节在上节的基础上使用Axis2自带的工具生成客户 ...
- MyBatis插入数据之后返回插入记录的id
MyBatis插入数据的时候,返回该记录的id<insert id="insert" keyProperty="id" useGeneratedKeys= ...