【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 ...
随机推荐
- SpringCloud---API网关服务---Spring Cloud Zuul
1.概述 1.1 微服务架构出现的问题 及 解决: 1.1.1 前言 每个微服务应用都提供对外的Restful API服务,它通过F5.Nginx等网络设备或工具软件实现对各个微服务的路由与负载 ...
- c++ 常用的遍历,删除,分割等等文件处理函数代码实现
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9622851.html 删除文件目录函数: void myDeleteDirectory(CSt ...
- blender
Blender 是一款开源的跨平台全能三维动画制作软件,提供从建模.动画.材质.渲染.到音频处理.视频剪辑等一系列动画短片制作解决方案. 教程 https://www.bilibili.com/vid ...
- LookupError: unknown encoding: cp65001的问题
在CMD中进入Python2命令行交互模式,输入代码报错: LookupError: unknown encoding: cp65001 解决方法: 在CMD中设置 Python2 的环境变量 PYT ...
- mysql之调优概论
一 简介 咱们先不说cpu的频率,内存的大小(这个和索引一样重要,但不是本文讨论的内容),硬盘的寻道时间.想起mysql的调优,最起码的必须知道explain执行计划,慢sql日志,老旧的profi ...
- Git中.gitignore, 忽略追踪
在目录下 创建: .gitignore文件,将不需要被追踪的文件地址, 写在该文件中, 此时git软件就不会追踪列出的文件进行版本同步: windows不允许创建没有文件名的文件,可以用编辑器创建.g ...
- Firebird Internal Function
火鸟自定义内置函数,方便.强大. 特点:只可以返回单值,不能返回多行. 若想返回多行table,可以定义存储过程 Procedure,用suspend返回. 自定义内置函数,示例:返回当前批次号. c ...
- Oracle ORA-27102的解决办法(out of memory)
原文出自:https://blog.csdn.net/seesun2012 Oracle ORA-27102:out of memory 错误解决办法(简单粗暴,100%能解决内存占用问题) -前置: ...
- Expression Blend实例中文教程(5) - 布局控件快速入门StackPanel,ScrollViewer和Border
上一篇,介绍了Canvas布局控件在Blend中的使用.本篇继续介绍布局控件StackPanel,ScrollViewer和Border. 相对于Grid和Canvas来说,StackPanel,Sc ...
- Hibernate生成数据库表
首先创建实体类 import java.util.Date; public class ProductionEntity { public Integer getId() { return id; } ...