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 ...
随机推荐
- 本地连接远程Oracle数据库
由于项目开发测试,需要在本地连接远程的Oracle数据库 之前搭过环境,但是重装了系统,现在又重新装一遍 软件安装 连接远程Oracle需要两个软件: 一个Oracle客户端,instantclien ...
- React 实践记录 03 React router
Introduction 本文主要参考了react router 的官方文档. React Router是一套完整的配合React的路由解决方案,可能你已经知道前端路由,或者知道后端有路由的概念,如下 ...
- 关于HTML5手机端页面缩放的问题
通常在写HTML5手机端页面的时候,我们会发现页面所显示元素的比例不正确,那此时我们需要添加的就是: <meta name="viewport" content=" ...
- BeanUtils 工具类
一.BeanUtils 概述 BeanUtils 是阿帕奇提供的一套专门用于将一些数据封装到java对象中的工具类; 名词:javaBean:特定格式的java类称为java ...
- Xilinx FPGA结构
FPGA是什么?FPGA是现场可编程逻辑阵列,由可编程逻辑资源(LUT和 REG),可编程连线,可编程I/O构成.Xilinx的FPGA的基本结构是一样的,但随着半导体工艺的发展,FPGA的逻辑容量越 ...
- ZOJ 3627 Treasure Hunt II (贪心,模拟)
题意:有n个城市并排着,每个城市有些珠宝,有两个人站在第s个城市准备收集珠宝,两人可以各自行动,但两人之间的距离不能超过dis,而且每经过一个城市就需要消耗1天,他们仅有t天时间收集珠宝,问最多能收集 ...
- The Django Book 第三章 试图和URL配置
之前自学Django也有一段时间了,再过一个月就要入职新公司了(Python Django开发),即使现在还在入门级徘徊,再好好把Django基础过一遍吧. The Django Book 第三章 试 ...
- (转)SpringMVC学习(二)——SpringMVC架构及组件
http://blog.csdn.net/yerenyuan_pku/article/details/72231385 相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上 ...
- C03 程序逻辑
程序逻辑 运算符 顺序结构 选择结构 循环结构 运算符 赋值运算符:= 比较运算符:>.<.==. >=.<=.!= 逻辑运算符:&&.||.! 顺序结构 在C ...
- go get 升级所有
go get -u all go get -u go mod update go get -u full_package_name go get -u github.com/... // ('. ...