Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal
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.
递归构建。
思路就是: preorder可以定位到根结点,inorder可以定位左右子树的取值范围。
1. 由preorder得到根结点;把preorder第一个点删掉;
2. 先建左子树;再建右子树;
通过一个区间来表示左右子树的取值范围。因为inorder左右子树的范围都是连续的。中间就是root。
class Solution {
public:
TreeNode *buildTree(vector<int> &preorder, vector<int> &inorder) {
return recursive(preorder, inorder, , inorder.size() - );
}
TreeNode* recursive(vector<int> &preorder, vector<int> &inorder, int s, int e) {
if (s > e) return NULL;
if (preorder.empty()) return NULL;
TreeNode *root = new TreeNode(preorder.front());
preorder.erase(preorder.begin());
int i = s;
for (; i <= e && inorder[i] != root->val; ++i);
root->left = recursive(preorder, inorder, s, i - );
root->right = recursive(preorder, inorder, i + , e);
}
};
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.
和上面类似。有两点不同。
1. postorder,最后一个元素是根结点。
2. 先构建右子树,再构建左子树。
class Solution {
public:
TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) {
return recursive(postorder, inorder, , inorder.size() - );
}
TreeNode* recursive(vector<int> &postorder, vector<int> &inorder, int s, int e) {
if (s > e) return NULL;
if (postorder.empty()) return NULL;
TreeNode *root = new TreeNode(postorder.back());
postorder.pop_back();
int i = s;
for (; i <= e && inorder[i] != root->val; ++i);
root->right = recursive(postorder, inorder, i + , e);
root->left = recursive(postorder, inorder, s, i - );
}
};
Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal的更多相关文章
- 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 ...
- 105. Construct Binary Tree from Inorder and preorder Traversal
/* * 105. Construct Binary Tree from Inorder and preorder Traversal * 11.20 By Mingyang * 千万不要以为root ...
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告
Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [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] Construct Binary Tree from Inorder and Pretorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 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]Construct Binary Tree from Inorder and Postorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...
- [Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树
Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume th ...
随机推荐
- Android开发——网易云音乐使用的开源组件集合
前言 网易云音乐Android版从第一版使用到现在,全新的 Material Design 界面,更加清新.简洁.同样也是音乐播放器开发者,我们确实需要思考,相同的功能,会如何选择.感谢开源,让我们有 ...
- 使用code::blocks编译windows的dll链接库
因为机子上没有安装Visual Studio,所以找到了一种通过code::blocks编译dll的方式,踩到的坑是code::blocks默认的compiler是32位的,这样编译出的dll也是32 ...
- day39---mysql基础三
1.索引: 字典得目录,便于数据查找. 原理:将列信息存储在其相关的文件,这些信息使用便于检索的方式如B-tree.哈希来存储 索引的分类: 普通所有:name,只能帮助查找 唯一索引:name,帮助 ...
- 用Python 3写的一个Spider小爬虫(使用内置urllib模块and正则表达式)
用Python写了一个Spider小爬虫,爬一爬斗鱼“王者荣耀”在线直播的主播及人气
- kickstart配置文件详解和system-config-kickstart
kickstart是什么 许多系统管理员宁愿使用自动化的安装方法来安装红帽企业 Linux.为了满足这种需要,红帽创建了kickstart安装方法.使用kickstart,系统管理员可 ...
- 【LeetCode】汉明距离(Hamming Distance)
这道题是LeetCode里的第461道题. 题目描述: 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, ...
- PAT——乙级1016
乙级PAT的1016 乙级的题相对比较简单,我也是主要联系写代码的格式,而不是联系算法. 1016 部分A+B (15 point(s)) 正整数 A 的“DA(为 1 位整数)部分”定义为由 ...
- sizeof 数组名字,数组指针
int *a = new int[15]; sizeof(a)//在64位机器上,8 sizeof(a)/sizeof(int) //2 ----- int a[15]; sizeof(a)//15* ...
- Hibernate命名策略及配置
hibernate 表 命名策略 分类: hibernate2013-02-27 18:46464人阅读评论(0)收藏举报 Hibernate注释下的自定义架构实 ...
- Java精确测量代码运行时间 代码执行时间 纳秒 nanoTime
Java精确测量代码运行时间: long startTime = System.nanoTime(); //開始時間 for(int i = 0;i<100 ...