Leetcode:1008. 先序遍历构造二叉树
Leetcode:1008. 先序遍历构造二叉树
Leetcode:1008. 先序遍历构造二叉树
思路
既然给了一个遍历结果让我们建树,那就是要需要前序中序建树咯~
题目给的树是一颗BST树,说明中序历遍是有序的。最简单的想法自然是先排序再建树。
但是排序似乎是不需要的,因为BST左子树必小于右子树,于是我们只需要确定哪个点比根节点要大,就能说明右子树是从哪里开始的。
先序遍历无法确定的东西有一个被确定了,左子树的范围就很简单了啊。
于是不需要先排序,只要历遍一下就可以了。降低了一点复杂度。
Talk is cheap . Show me the code .
/**
* 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* built(int root,int start,int end,vector<int> preOrder){
if(start>end) return NULL;
TreeNode* treenode=new TreeNode(preOrder[root]);
int index=start;
while(index<=end&&preOrder[root]>=preOrder[index]) index++;
treenode->left=built(root+1,start+1,index-1,preOrder);
treenode->right=built(root+index-start,index,end,preOrder);
return treenode;
}
TreeNode* bstFromPreorder(vector<int>& preorder) {
TreeNode *node=built(0,0,preorder.size()-1,preorder);
return node;
}
};
Leetcode:1008. 先序遍历构造二叉树的更多相关文章
- Leetcode 1008. 先序遍历构造二叉树
1008. 先序遍历构造二叉树 显示英文描述 我的提交返回竞赛 用户通过次数169 用户尝试次数183 通过次数171 提交次数247 题目难度Medium 返回与给定先序遍历 preorder ...
- 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 ...
- LintCode2016年8月8日算法比赛----中序遍历和后序遍历构造二叉树
中序遍历和后序遍历构造二叉树 题目描述 根据中序遍历和后序遍历构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下 ...
- 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 t ...
- [Swift]LeetCode889. 根据前序和后序遍历构造二叉树 | Construct Binary Tree from Preorder and Postorder Traversal
Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...
- [Swift]LeetCode1008. 先序遍历构造二叉树 | Construct Binary Search Tree from Preorder Traversal
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that ...
- leetcode-106-从中序和后序遍历构造二叉树
题目描述: 方法一:O(n) O(n) class Solution: def buildTree(self, inorder: List[int], postorder: List[int]) -& ...
- leetcode-105-从前序与中序遍历构造二叉树
题目描述: 方法一: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.va ...
- [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 ...
随机推荐
- C++标准模板库(STL)——map常见用法详解
map的定义 map<typename1, typename2> mp; map需要确定映射前类型和映射后类型,所以需要在<>内填写两个类型,第一个是键的类型,第二个是值的类型 ...
- 一、部署监控服务器--安装LNMP环境
1.要求: 本案例要求部署-台Zabbix监控服务器, -台被监控主机,为进一步执行具体的监控任务做准备:1.安装LNMP环境2.源码安装Zabbix3.安装监控端主机,修改基本配置4.初始化Zabb ...
- 选择合适Redis数据结构,减少80%的内存占用
redis作为目前最流行的nosql缓存数据库,凭借其优异的性能.丰富的数据结构已成为大部分场景下首选的缓存工具. 由于redis是一个纯内存的数据库,在存放大量数据时,内存的占用将会非常可观.那么在 ...
- 如果你这么去理解HashMap就会发现它真的很简单
Java中的HashMap相信大家都不陌生,也是大家编程时最常用的数据结构之一,各种面试题更是恨不得掘地三尺的去问HashMap.HashTable.ConcurrentHashMap,无论面试题多么 ...
- SprignBoot是如何访问工程目录下的静态资源?
目录 1.牛刀小试 1.1 图片静态资源的访问 1.2 为静态资源添加访问前缀 1.3 WelCome Page 的奇妙跳转 2.那么,SpringBoot是如何做到的呢? 1. ...
- 把新建的vue项目上传到码云
1:在码云上建一个仓库(使用Readme文件初始化这个项目的勾取消掉) 2:在项目文件中打开git命令窗口(如下图),命令git init 初始化git仓库 运行之后有一个.git文件夹 现在用vsc ...
- 低代码开发LCDP,Power Apps系列 - 搭建入职选购电脑设备案例
低代码简介 上世纪八十年代,美国就有一些公司和实验室开始了可视化编程的研究,做出了4GL"第四代编程语言",到后来衍生成VPL"Visual Programming La ...
- 30、LNAP(php和nginx相关优化)
30.1.php-fpm.conf参数优化: [global] pid = run/php-fpm.pid #php后台运行pid路径 error_log = log/php-fpm.log #php ...
- 7.4、Horizon部署
1.horizon介绍: horizon只需要连上keystone即可: 这里在控制节点controller进行操作: 提示:horizon和keystone运行时使用的都是apache软件,如果都装 ...
- MyBatis温故而知新-底层运行原理
准备工作 public class MainClass { public static void main(String[] args) throws Exception { String resou ...