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/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.
思想: 迭代。
说明: 这类问题,要求一个提供根节点,然后另一个序列(中序序列)可依据根节点分出左右子树。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
TreeNode *createTree(vector<int> &inorder, int start, int end, vector<int> &postorder, int start2, int end2) {
if(start > end || start2 > end2) return NULL;
TreeNode *root = new TreeNode(postorder[end2]);
int i;
for(i = start; i <= end; ++i)
if(inorder[i] == postorder[end2]) break;
// if(i > end) throws std::exception("error");
root->left = createTree(inorder, start, i-1, postorder, start2, start2 + i-start-1);
root->right = createTree(inorder, i+1, end, postorder, start2+i-start, end2-1);
return root;
} class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return createTree(inorder, 0, inorder.size()-1, postorder, 0, postorder.size()-1);
}
};
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.
思想: 同上。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
TreeNode* createTree(vector<int> &preorder, int start, int end, vector<int> &inorder, int start2, int end2) {
if(start > end || start2 > end2) return NULL;
TreeNode *root = new TreeNode(preorder[start]);
int i;
for(i = start2; i <= end2; ++i)
if(preorder[start] == inorder[i]) break;
root->left = createTree(preorder, start+1, start+i-start2, inorder, start2, i-1);
root->right = createTree(preorder, start+i-start2+1, end, inorder, i+1, end2);
return root;
}
class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return createTree(preorder, 0, preorder.size()-1, inorder, 0, inorder.size()-1);
}
};
36. 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 traversal of a tree, construct the binary tree. Note:You may assume that ...
- 【LeetCode OJ】Construct Binary Tree from Inorder and Postorder Traversal
Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-trav ...
- leetcode-1006 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 ...
- 【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- [LeetCode-21]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 ...
- 【构建二叉树】02根据中序和后序序列构造二叉树【Construct Binary Tree from Inorder and Postorder Traversal】
我们都知道,已知中序和后序的序列是可以唯一确定一个二叉树的. 初始化时候二叉树为:================== 中序遍历序列, ======O=========== 后序遍 ...
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume tha ...
- LeetCode OJ 106. 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 ...
- Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
随机推荐
- redhat6.4上build storm 0.9.0.1
1.安装mvn 2.下载源代码 3.build mvn package 过程中出现问题,clojars.org 访问不了.通过私服映射clojars.org并在pom.xml中将dependency的 ...
- Google加强版权保护
在版权保护方面,我们一直是反面教材,而在场面上Google早已退出我们的世界,所以Google的加强版权保护对国内的互联网不会有太多的影响,即便无法在Google搜索到我们需要的XXX软件破解版,百度 ...
- Xcode5 取消ARC
终于开心的装上Xcode5,主管马上发布新的任务,开始新的项目,各种开心,终于可以换个界面看看了. 可是谁知第一步创建项目就开始悲剧了,居然没有地方可以选择非ARC了,真是肿么个情况呀,查了一下,万能 ...
- 需要使用id内省方法--responsesToSelector: 的两个地方
第一个: 当从数组中取出对象,并且需要执行某个方法时,最好使用responsesToSelector:判断该对象是否可以 执行该方法.因为在OC数组中,取出的对象都是 id 类型的. 第二个: 在MV ...
- HDU 1508 DP
题意:规定一个数列 = {这个数的质因子只能包括2,3,5,7},求第n个数字是多少: 思路:暴力打表,然后只粘数据,虽然过了,但是正解其实是DP,每一个数字都是由某一个该数列里的某一个数字乘以2,3 ...
- SQL视图与触发器
视图(虚拟的表) select查询出来的结果集可以用as起别名当作虚拟表来使用 视图只能添加使用不能添加修改 视图不能建在其他视图上,只能一其他实体表作为基础 视图表的数据会随实体表的变动而变动 视图 ...
- 使用Object.observe 实现数据绑定
Object.observe API概述 最近,JavaScript的MVC框架在Web开发届非常盛行.在实现MVC框架的时候,一个非常重要的技术就是数据绑定技术.如果要实现模型与视图的分离,就必须要 ...
- python数据结构与算法——链表
具体的数据结构可以参考下面的这两篇博客: python 数据结构之单链表的实现: http://www.cnblogs.com/yupeng/p/3413763.html python 数据结构之双向 ...
- Jena Fuseki 101
前言 正如其承诺的那样 Expose your triples as a SPARQL end-point accessible over HTTP. Fuseki provides REST-sty ...
- Kraken taxonomic sequence classification system
kraken:是一个将分类标签打到短DNAreads上的分类序列器.