【Leetcode】【Medium】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.
解题思路:
给出一个二叉树的先序和中序遍历结果,还原这个二叉树。
对于一个二叉树:
1
/ \
2 3
/ \ / \
4 5 6 7
先序遍历结果为:1 2 4 5 3 6 7
中序遍历结果为:4 2 5 1 6 3 7
由此可以发现规律:
1、先序遍历的第一个字符,就是根结点(1)
2、发现根节点后,对应在中序遍历中的位置,则在中序遍历队列中,根节点左边的元素构成根的左子树,根的右边元素构成根的右子树;
3、递归的将左右子树也按照上述规律进行构造,最终还原二叉树。
代码:
/**
* Definition for a binary tree node.
* 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) {
return buildSubtree(preorder, inorder, ,
preorder.size()-,
, inorder.size()-);
} TreeNode* buildSubtree(vector<int>& preorder, vector<int>& inorder,
int p_left, int p_right, int i_left, int i_right) {
if (p_left > p_right || i_left > i_right)
return NULL; int root = preorder[p_left];
TreeNode* node = new TreeNode(preorder[p_left]); int range = ;
for (int j = i_left; j <= i_right; ++j) {
if (root == inorder[j]) {
range = j - i_left;
break;
}
} node->left = buildSubtree(preorder, inorder,
p_left + , p_left + range,
i_left, i_left + range - );
node->right = buildSubtree(preorder, inorder,
p_left + range + , p_right,
i_left + range + , i_right);
return node;
}
};
【Leetcode】【Medium】Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章
- 【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 ...
- 【题解二连发】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 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: 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 ...
- 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 ...
随机推荐
- debian sudo
apt-get install sudo vi /etc/sudoers add CentOS 7 root ALL=(ALL) ALL Debian root ALL=(ALL:ALL) ALL 按 ...
- Zookeeper概念学习系列之zab协议
不多说,直接上干货! 上一章讨论了paxos算法,把paxos推到一个很高的位置. Zookeeper概念学习系列之paxos协议 但是,paxos有没有什么问题呢?实际上,paxos还是有其自身的缺 ...
- MySql的运算符
数据库中的表结构确立后,表中的数据代表的意义就已经确定.而通过MySQL运算符进行运算,就可以获取到表结构以外的另一种数据.例如,学生表中存在一个birth字段,这个字段表示学生的出生年份.而运用My ...
- HDU 5694——BD String——————【递归求解】
BD String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- Apache 反向代理 丢失Authorization
我后端API的服务器是Tomcat,而后端API验证是通过存放在头部Authorization的token值进行验证的. 我在测试Apache作为前端html解析的服务器时, 利用反向代理,把Api请 ...
- bzoj 4540: [Hnoi2016]序列
Description 给定长度为n的序列:a1,a2,-,an,记为a[1:n].类似地,a[l:r](1≤l≤r≤N)是指序列:al,al+1,-,ar- 1,ar.若1≤l≤s≤t≤r≤n,则称 ...
- javascript 中合并排序算法 详解
javascript 中合并排序算法 详解 我会通过程序的执行过程来给大家合并排序是如何排序的... 合并排序代码如下: <script type="text/javascript& ...
- Table Code
post.PostToTags.Where(t => tagArray.Contains(t.PostTag.Name, comparerWihtoutCases) && !t. ...
- BEM样式使用规范
BEM 是 Block(块) Element(元素) Modifier(修饰器)的简称 使用BEM规范来命名CSS,组织HTML中选择器的结构,利于CSS代码的维护,使得代码结构更清晰(弊端主要是名字 ...
- PHP+MySql实现图书管理系统
这个图书管理系统是我学完PHP时写的一个练手项目,功能参考了自己学校的图书管理系统.为了锻炼自己的动手能力以及加深对代码的理解,前端和后端均由自己完成,前端使用了一些基本的框架(毕竟我主攻后端开发方向 ...