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 ...
随机推荐
- 【NX二次开发】根据根据坐标系、对象旋转视图旋转视图uc6434
uc6434 (); //旋转视图 参数1:如果输入""则旋转当前工作视图参数2:1.按照ABS旋转视图.2.按照WCS选择视图.3.按照参数3旋转视图.4.按照参数4旋转视图参数 ...
- 使用Flutter设计一个好看的"我"页面
近期遇到一些很烦的琐事,状态比较down,很多原本计划好的事情都耽搁了,实在是难顶-- 看到后台一直有朋友问怎么博客和公众号没有更新,所以我忙完得闲就来更了! 前言 起因是最近重拾以前的旧项目(业余做 ...
- 第11章 PADS功能使用技巧(2)-最全面
原文链接点击这里 七.Flood与Hatch有什么区别? 我们先看看PADS Layout Help 文档是怎么说的,如下图所示: 从检索到的帮助信息,我们可以得到Hatch与Pour的区别,原文如下 ...
- Redis i/o timeout
1.背景 公司项目使用国外ucloud云,发现公司业务服务器时常连接redis服务,发生i/o timeout的问题.研发以及服务器侧查看没有异常,反馈给ucolud解决问题.所以这里做一个记录. 2 ...
- 面向.NET开发人员的Dapr- actors 构建块
原文地址:https://docs.microsoft.com/en-us/dotnet/architecture/dapr-for-net-developers/actors The actor m ...
- 关于TreeView的实例
前台代码 (只需要有TreeView控件, 添加ID,其他默认生成) <form id="form1" runat="server"> <di ...
- Vulkan移植GPUImage的安卓Demo展示
演示Android apk下载 需要Android 8以上. 先看效果图,大约一百多种滤镜,有超过一半的滤镜有参数设置,其参数调整界面使用反射自动生成与绑定. 如下每种选择一些进行展示. 视觉效果 图 ...
- Raspberry Pi:树莓派开发板配置USB启动系统
准备材料 树莓派4B U盘 TF卡 树莓派基础镜像2020-08-20稳定版(这个系统是必须的并拷录在TF卡) Kali树莓派系统(这个是我想要学习的系统,大家可以准备自己的系统,拷录在U盘的) SD ...
- Linux:CentOS-7常用命令
查看进程 1. ps -ef | grep #查看进程 ps -ef | grep 名称 #示例 ps -ef | grep docker 2. ps aux #当前所有进程信息 ps aux VSZ ...
- Mybatis学习(3)实现数据的增删改查
前面已经讲到用接口的方式编程.这种方式,要注意的一个地方就是.在User.xml 的配置文件中,mapper namespace="com.yihaomen.mybatis.inter.I ...