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 ...
随机推荐
- java 全端开源 电商系统 springboot uniapp 小程序 前后端分离 高可用
Lilishop B2B2C商城系统 官方公众号 & 开源不易,如有帮助请点Star 所有jar包均可从maven中央仓库下载,无二次封装jar包,全端开源,无后门,无监控. 介绍 官网:ht ...
- Win10 下python_appium的Android手机自动化环境搭建
前提: 已经安装好了Java环境,且配置了环境变量 已经安装python3.8.2,已经安装pycham. 一.安装appium_client ,pycham中也需要安装 二.安装node.js(需要 ...
- [源码解析] 深度学习分布式训练框架 horovod (4) --- 网络基础 & Driver
[源码解析] 深度学习分布式训练框架 horovod (4) --- 网络基础 & Driver 目录 [源码解析] 深度学习分布式训练框架 horovod (4) --- 网络基础 & ...
- Pytest测试框架入门到精通(一)
Python测试框架之前一直用的是unittest+HTMLTestRunner,听到有人说Pytest很好用,所以这边给大家介绍一下Pytest的使用 pytest是一个非常成熟的全功能的Pytho ...
- NOIP模拟测试3「序列·熟练剖分·建造游乐园(play)」
---恢复内容开始--- 序列 刚调出来样例就A了,假装是水题. 因为是乱序,我们要求出来每两项之间最小公比,而不是直接比 求出来每两项之间最小公比,然后扫一遍就完了.(还要注意重复情况) 那么问题就 ...
- 注册中心ZooKeeper,Eureka,Consul,Nacos对比
简介 服务注册中心本质上是为了解耦服务提供者和服务消费者.对于任何一个微服务,原则上都应存在或者支持多个提供者,这是由微服务的分布式属性决定的.更进一步,为了支持弹性扩缩容特性,一个微服务的提供者 ...
- SpringBoot项目创建流程--SpringMVC
SpringBoot项目创建步骤 1. 创建SpringBoot工程 (1) File → New → Project → Spring Initilizr (2) Name:MySpringBoot ...
- 浅析uniapp
前端跨平台框架 之uniapp入门浅析 技术的发展总日新月异,处在风口,前端技术的发展尤为迅速,跨平台的概念也在前端流行起来.从最早期PhoneGap.lonic.Cordova,到近年来的Reac ...
- 什么是WAF?
1.什么是Web Application Firewall(WAF)? WAF或Web Application Firewall通过过滤和监控Web应用程序与Internet之间的HTTP流量来帮助保 ...
- 第13章:Kubernetes 鉴权框架与用户权限分配
1.Kubernetes的安全框架 访问K8S集群的资源需要过三关:认证.鉴权.准入控制 普通用户若要安全访问集群API Server,往往需要证书.Token或者用户名+密码:Pod访问,需要Ser ...