leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
1、
Construct Binary Tree from Inorder and Postorder Traversal
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
代码:
- class Solution {
- public:
- TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
- TreeNode* root = NULL;
- int len1 = inorder.size();
- int len2 = postorder.size();
- if(len1<1 || len2<1 || len1!=len2){
- return root;
- }
- return buildTreeCore(inorder,0,len1-1,postorder,0,len2-1);
- }
- TreeNode* buildTreeCore(vector<int>&inorder, int startIndex1, int endIndex1, vector<int>&postorder, int startIndex2, int endIndex2){
- if(endIndex1 < startIndex1 ){
- return NULL;
- }
- TreeNode* root = new TreeNode(postorder[endIndex2]);
- int index = 0;
- for(int i=startIndex1; i<=endIndex1; ++i){
- if(inorder[i] == postorder[endIndex2]){
- index = i;
- break;
- }
- }
- int leftLen = index-startIndex1;
- TreeNode* leftNode = NULL;
- TreeNode* rightNode = NULL;
- leftNode = buildTreeCore(inorder,startIndex1,index-1,postorder,startIndex2,startIndex2+leftLen-1);
- rightNode = buildTreeCore(inorder,index+1,endIndex1,postorder,startIndex2+leftLen,endIndex2-1);
- root->left = leftNode;
- root->right = rightNode;
- return root;
- }
- };
2、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.
- class Solution {
- public:
- TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
- TreeNode* root = NULL;
- int len1 = inorder.size();
- int len2 = preorder.size();
- if(len1<1 || len2<1 || len1!=len2){
- return root;
- }
- return buildTreeCore(inorder,0,len1-1,preorder,0,len2-1);
- }
- TreeNode* buildTreeCore(vector<int>&inorder, int startIndex1, int endIndex1, vector<int>&preorder, int startIndex2, int endIndex2){
- if(endIndex1 < startIndex1 ){
- return NULL;
- }
- TreeNode* root = new TreeNode(preorder[startIndex2]);
- int index = 0;
- for(int i=startIndex1; i<=endIndex1; ++i){
- if(inorder[i] == preorder[startIndex2]){
- index = i;
- break;
- }
- }
- int leftLen = index-startIndex1;
- TreeNode* leftNode = NULL;
- TreeNode* rightNode = NULL;
- leftNode = buildTreeCore(inorder,startIndex1,index-1,preorder,startIndex2+1,startIndex2+leftLen);
- rightNode = buildTreeCore(inorder,index+1,endIndex1,preorder,startIndex2+leftLen+1,endIndex2);
- root->left = leftNode;
- root->right = rightNode;
- return root;
- }
- };
leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f的更多相关文章
- 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 Week14]Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告
[LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: ...
- 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 Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- 关于Python的装饰器
false 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE /* Style Definitions */ table.MsoNormalTable {m ...
- java 获取config 配置文件
static ResourceBundle PropertiesUtil = ResourceBundle.getBundle("config"); public static S ...
- 【BZOJ 1208】[HNOI2004]宠物收养所
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用set搞. (因为规定了不会有相同特点值的东西. 所以可以不用multiset. 那么每次用lower_bound找离它最近的配对 ...
- HDU 2222 Keywords Search AC自己主动机入门题
单词统计的题目,给出一些单词,统计有多少单词在一个文本中出现,最经典的入门题了. AC自己主动机的基础: 1 Trie. 以这个数据结构为基础的,只是添加一个fail指针和构造fail的函数 2 KM ...
- oracle 11g RAC手动卸载grid,no deinstall
1.通过root用户进入到grid的ORACLE_HOME [root@db01]# source /home/grid/.bash_profile [root@db01]# cd $ORACLE_H ...
- js html 事件冒泡
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- 智课雅思短语---五、 in contrast / on the contrary
智课雅思短语---五. in contrast / on the contrary 一.总结 一句话总结:相反 in contrast / on the contrary. 1.replace/ su ...
- 1.MySQL与MongoDB的操作对比,以及区别
转自:https://www.cnblogs.com/chris-oil/p/4982490.html MySQL与MongoDB都是开源的常用数据库,但是MySQL是传统的关系型数据库,MongoD ...
- django session深入
转至原文 https://www.cnblogs.com/zhaof/p/6281468.html 基于cookie做用户验证时:敏感信息不适合放在cookie中 session依赖cookie s ...
- [COI2007] [luogu P1823] Patrik 音乐会的等待 解题报告 (单调栈)
题目链接:https://www.luogu.org/problemnew/show/P1823 题目: N个人正在排队进入一个音乐会.人们等得很无聊,于是他们开始转来转去,想在队伍里寻找自己的熟人. ...