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 ...
随机推荐
- Golang Heap 源码剖析
堆原理解析 堆一般指二叉堆.是使用完全二叉树这种数据结构构建的一种实际应用.通过它的特性,分为最大堆和最小堆两种. 如上图可知,最小堆就是在这颗二叉树中,任何一个节点的值比其所在子树的任意一个节点都要 ...
- 小白学k8s(7)helm[v3]使用了解
helm使用 什么是helm 安装helm Helm V2 & V3 架构设计 配置kube config helm使用 添加仓库 helm安装nginx helm的核心概念 Chart Co ...
- Java源码分析:Guava之不可变集合ImmutableMap的源码分析
一.案例场景 遇到过这样的场景,在定义一个static修饰的Map时,使用了大量的put()方法赋值,就类似这样-- public static final Map<String,String& ...
- kubernetes之副本控制器(RC/RS)
1.了解ReplicationController ReplicationController是一种kubernetes资源,可确保它的pod始终保持运行状态. 如果pod因任何原因消失(例如节点从集 ...
- Redis在linux系统中的优化
通常来看,Redis开发和运维人员更加关注的是Redis本身的一些配置优化,例如AOF和RDB的配置优化.数据结构的配置优化等,但是对于操作系统是否需要针对Redis做一些配置优化不甚了解或者不太关心 ...
- 13、ssh跳板机
13.1.前提条件: 1.跳板机服务器和其它服务器建立了ssh秘钥登录: 2.目前的环境: (1)各服务器上都建立了lc用户,并给于sudo (lc ALL= NOPASSWD:ALL )权限,lc就 ...
- MindSpore模型精度调优实战:如何更快定位精度问题
摘要:为大家梳理了针对常见精度问题的调试调优指南,将以"MindSpore模型精度调优实战"系列文章的形式分享出来,帮助大家轻松定位精度问题,快速优化模型精度. 本文分享自华为云社 ...
- gRPC 拦截器能做些什么?
什么是拦截器? 拦截器是一种横切维度的功能延展. 具象说明一下,高速收费站就是一种拦截器.它可以做什么?收费,查证,交通控制等等,面向所有穿行过往的车辆. gRPC 拦截器主要分为两种:客户端拦截器( ...
- 学会这些CSS技巧让你写样式更加丝滑
目录 1,前言 1,calc() 2,min() 3,max() 4,clamp() 5,gap 6,writing-mode 1,前言 记录一些很好用的css属性 1,calc() calc()函数 ...
- docker安装和配置nginx
配置nginx docker配置nginx 本机ip是192.168.0.200 docker pull nginx 配置nginx主机 vi /root/docker/nginx/nginx01.c ...