[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 ...
随机推荐
- Slava and tanks 877C
C. Slava and tanks time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- sourcetree 添加私钥
参考网址: https://www.jianshu.com/p/2a4a39e3704f
- 对String值不可变的理解以及String类型的引用传递问题
今天复习java时,突然注意到了一句以前没有注意过的一句话,String 是final修饰的,其值是不可变的.当时看的一脸懵逼,String str = "abc"; str = ...
- maven ,添加加密算法,使用
1:消息摘要:(数字指纹):既对一个任意长度的一个数据块进行计算,产生一个唯一指纹.MD5/SHA1发送给其他人你的信息和摘要,其他人用相同的加密方法得到摘要,最后进行比较摘要是否相同. MD5(Me ...
- jenkins 自动触发
在gitlab上配置连接jenkins ,将Jenkins的Secret token 与Build URL 复制到gitlab中 在settings标签下面,找到OutBound Request,勾选 ...
- js高级-浏览器事件循环机制Event Loop
JavaScript 是队列的形式一个个执行的 同一时间只能执行一段代码,单线程的 (队列的数据结构) 浏览器是多线程的 JavaScript执行线程负责执行js代码 UI线程负责UI展示的 Jav ...
- JS----获取DOM元素的方法(8种)
什么是HTML DOM 文档对象模型(Document Object Model),是W3C组织推荐的处理可扩展置标语言的标准编程接口.简单理解就是HTML DOM 是关于如何获取.修改.添加或删除 ...
- java NIO 文章
http://tutorials.jenkov.com/java-nio/ 总结nio nio是非阻塞的,一个线程可以管多个Channel,每个channel可以处理bytebuffer 而no是阻塞 ...
- Vue 中select option默认选中的处理方法
在做泰康项目的时候有个需求就是要给select默认选中的样式我的处理方法有两个 1.直接将默认值给 selectedOption <select v-model="selectedO ...
- Mybatis返回List<Map<K,V>>
最终映射的字段名 会被作为 hashMap 的 key , <!-- TODO 测试返回 HashMap--> <resultMap id="testResultMap&q ...