[LeetCode_105]Construct Binary Tree from Preorder and Inorder Traversal
题目链接
https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
题意
由二叉树的先序遍历和中序遍历建树
思路
理解建树过程;使用递归,递归关键:清楚递归过程,明确函数参数、返回值,写终止条件。
此外,注意空树的特判。
其他点
1 熟悉find()、assign()的使用。
2 使用
struct TreeNode *pNode =new TreeNode(*preRootIter);
而不是
struct TreeNode node=TreeNode(*preRootIter);
struct TreeNode* pNode=node;
否则造成assign的时候树中某些节点值被改变。
todo
上述问题原因待查。
应该最后要遍历树释放内存吧。
代码
#include <vector>
#include <iostream>
using namespace std;
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.size()){
return NULL;
}
else{
struct TreeNode* root=buildT(preorder,inorder);
return root;
}
}
private:
TreeNode* buildT(vector<int> preorder,vector<int> inorder){
vector<int>::iterator preRootIter=preorder.begin();
vector<int>::iterator midRootIter=find(inorder.begin(), inorder.end(), *preRootIter);
//create a new node
struct TreeNode *pNode =new TreeNode(*preRootIter);
// struct TreeNode node=TreeNode(*preRootIter);
// struct TreeNode* pNode=node;
//left child tree
vector<int>::size_type leftLen=midRootIter-inorder.begin();
vector<int>::iterator lPreBeg=preRootIter+1;
vector<int>::iterator lPreEnd=lPreBeg+leftLen;
if(!leftLen){
pNode->left=NULL;
}
else{
vector<int> preorderLeft;
preorderLeft.assign(lPreBeg, lPreEnd);
vector<int> inorderLeft;
inorderLeft.assign(inorder.begin(), midRootIter);
pNode->left=buildT(preorderLeft, inorderLeft);
}
//right child tree
vector<int>::size_type rightLen=inorder.end()-(midRootIter+1);
if(!rightLen){
pNode->right=NULL;
}
else{
vector<int> preorderRight;
vector<int>::iterator rPreBeg=lPreEnd;
vector<int>::iterator rPreEnd=rPreBeg+rightLen;
preorderRight.assign(rPreBeg, rPreEnd);
vector<int> inorderRight;
inorderRight.assign(midRootIter+1, inorder.end());
pNode->right=buildT(preorderRight, inorderRight);
}
return pNode;
}
};
int main(){
Solution solution;
vector<int> preorder={3,9,20,15,7};
vector<int> inorder={9,3,15,20,7};
struct TreeNode* tree=solution.buildTree(preorder,inorder);
return 0;
}
[LeetCode_105]Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 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 ...
- [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 ...
- 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...
随机推荐
- ActiveMQ 学习
链接: http://www.cnblogs.com/zhuxiaojie/p/5564187.html#autoid-1-0-0
- 25_ajax请求_使用fetch
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- redis 学习笔记2(集群之哨兵模式的使用)
redis3.0之前已经有了哨兵模式,3.0之后有了cluster(分片集群),官方不推荐使用!!主要原因是分片后单节点故障后需要实现手动分槽... 集群较为成熟的解决方案codis,公司使用的是哨兵 ...
- 微服务-springcloud
感觉微服务都差不多概念,最近稍微看了下springcloud,感觉入门还是很简单的,框架用用就那么回事,深入的话需要很多时间投入了 学一个东西,我推荐首先从概念上了解到他是做什么的,什么时候需要,基本 ...
- Spark读取配置(转)
转自:https://github.com/keepsimplefocus/spark-sourcecodes-analysis/blob/master/markdowns/Spark%E8%AF%B ...
- Android Studio 版本间区别
2.3.2 ->3.0.1 Gradle版本为4.1 com.android.tools.build:gradle:3.0.x Android Monitor 被换成了 Android P ...
- week06 07 创建RPC SERVER 换个镜像安装下载
RPC server 使用python类库 https://pypi.org/project/python-jsonrpc/ 和NPM 不一样 他没有global选项 他安装的就是全局的安装的类库叫p ...
- 法门扫地僧总结vue面试题(部分来源网络)
Front-End 前端开发工程师面试宝典! (本文部分有转载,不定期更新!) 前言(README.md) 本仓库是我整理的前端常见面试题,大部分由我整理,其中个别部分参考 ...
- highstock无图像
如果你的x轴是时间又是世纪秒的话又按以下设置的话,把xAxis的设置去掉试试看, 因为highstock会对世纪秒自动转换的 // xAxis: {// // max: 23, // min: 0, ...
- jenkins 自动触发
在gitlab上配置连接jenkins ,将Jenkins的Secret token 与Build URL 复制到gitlab中 在settings标签下面,找到OutBound Request,勾选 ...