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 &amp; Construct Binary Tree f的更多相关文章

  1. 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 ...

  2. [Leetcode Week14]Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/pr ...

  3. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    [LeetCode]106. Construct Binary Tree from Inorder and Postorder Traversal 解题报告(Python) 标签: LeetCode ...

  4. LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  5. 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

  6. 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: ...

  7. 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 ...

  8. 【题解二连发】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 ...

  9. Construct Binary Tree from Inorder and Postorder Traversal

    Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...

随机推荐

  1. react-native之文件上传下载

    目录 文件上传 1.文件选择 2.文件上传 1.FormData对象包装 2.上传示例 文件下载 最近react-native项目上需要做文件上传下载的功能,由于才接触react-native不久,好 ...

  2. [CTSC2007][APIO2007]数据备份Backup

    题目:BZOJ1150.codevs1615.洛谷P3620 题目大意:有n个点,k条链,每个点离原点有一定的距离.要你用k条链连接2k个点,使得k条链的长度最短. 解题思路:毕竟是CTSC级别的题目 ...

  3. 堆(Heap)-c实现

    这个堆的实现采用数组存储的完全二叉树实现. 最近有点烦躁,先是跳槽到了一个外包公司,感觉2016有点坑,另外一件事就是老婆怀孕了,但是在家里没人照顾,很担心. 这个堆的实现就暂时不优化了,基本的插入, ...

  4. 素数计数函数$\pi(x)\sim \Theta(\frac{x}{\log{x}})$的一个初等方法——素数定理的估计

    $\DeclareMathOperator{\lcm}{lcm}$ 本文的方法来源于GTM 190:"Problems in Algebraic Number Theory",给出 ...

  5. grep的使用【转】

    grep的作用是显示匹配一个或多个模式的文本行.时常会作为管道(|)的第一步,以便对匹配的数据作进一步处理.grep常用于查找和替换文本的.在传统上,grep有3个版本:grep.egrep(扩展gr ...

  6. Java 学习(11): 面向对象编程—继承(super,this)

    Java 继承 what: 继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为.子类从它的父类中继承可访问的数据域和方法,也 ...

  7. 面试题——ArrayList和LinkedList的区别

    List概括 先回顾一下List在Collection的框架图: 从图中可以看出: List是一个接口,他继承Collection接口,代表有序的队列. AbstractList是一个抽象类, ,它继 ...

  8. Qt之字典划词

    简述 相信大家都用过词典吧!因为英语不太好...O(∩_∩)O~,所以经常进行划词翻译! 简述 实现 效果 源码 更多参考 实现 原理:鼠标移至某单词之上,获取鼠标位置,然后在对应位置进行取词,翻译! ...

  9. 几周内搞定Java的10个方法

    不要将Java与JavaScript弄混了,Java的目标是“一次编译,到处调试”(呃,不对,是“到处运行”).简单来说,就是Java程序可以直接在任何设备上运行. Java语言是什么? 不管我们是否 ...

  10. httpd: Could not reliably determine the server&#39;s fully qualified domain name

    [root@luozhonghua sbin]# service httpd start Starting httpd: httpd: apr_sockaddr_info_get() failed f ...