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. 先序遍历构造二叉树的更多相关文章

  1. Leetcode 1008. 先序遍历构造二叉树

    1008. 先序遍历构造二叉树  显示英文描述 我的提交返回竞赛   用户通过次数169 用户尝试次数183 通过次数171 提交次数247 题目难度Medium 返回与给定先序遍历 preorder ...

  2. 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 ...

  3. LintCode2016年8月8日算法比赛----中序遍历和后序遍历构造二叉树

    中序遍历和后序遍历构造二叉树 题目描述 根据中序遍历和后序遍历构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下 ...

  4. 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 ...

  5. [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 ...

  6. [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 ...

  7. leetcode-106-从中序和后序遍历构造二叉树

    题目描述: 方法一:O(n) O(n) class Solution: def buildTree(self, inorder: List[int], postorder: List[int]) -& ...

  8. leetcode-105-从前序与中序遍历构造二叉树

    题目描述: 方法一: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.va ...

  9. [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 ...

随机推荐

  1. 【NX二次开发】NX内部函数,pskernel.dll文件中的内部函数

    pskernel.dll文件中的内部函数,含有部分pk函数,用法可以查看pk函数帮助: ADPAPE ADVXED APPTRA ATGETO ATTGEO BLECHK BLECRB BLECVR ...

  2. AS打包签名

    1.进入项目,然后点击菜单栏的Build  -->Generate  Signed APK... (如下图所示) 2.点击之后会出现下图,我这个是我以前有过KEY了,如果你以前没有过的话,都是空 ...

  3. 【春节歌曲回味 | STM32小音乐盒 】PWM+定时器驱动无源蜂鸣器(STM32 HAL库)

    l  STM32通过PWM与定时器方式控制无源蜂鸣器鸣响 l  STM32小音乐盒,歌曲进度条图形显示与百分比显示,歌曲切换 l  编程使用STM32 HAL库 l  IIC OLED界面编程,动画实 ...

  4. Netty 框架学习 —— 添加 WebSocket 支持

    WebSocket 简介 WebSocket 协议是完全重新设计的协议,旨在为 Web 上的双向数据传输问题提供一个切实可行的解决方案,使得客户端和服务器之间可以在任意时刻传输消息 Netty 对于 ...

  5. 7、resync实时备份

    sersync+rsync(增量,无差异备份),resync支持多线程,效果比inotify更好,配置思想和inotify很相似 7.1.在备份服务器上安装并配置rsync服务,实现nfs共享目录,可 ...

  6. ubuntu 替换某一内核模块

    流程 方法一 以下配置仅执行一次,并以 linux kernel 3.13.0 为例 $ cd ~ $ apt-get source linux-source-3.13.0 $ cd linux-3. ...

  7. 初识Sonarqube

    Sonarqube 官方网站地址: 官方网站地址:https://www.sonarqube.org/ Sonarqube 官方介绍: 使用 SonarQube 静态分析,您可以在一个地方衡量项目中所 ...

  8. WEB与游戏开发的一些区别

    WEB与游戏开发的一些区别 前言 ​ 最近由于在准备期末考,以及准备实习.其实都没好好写过博客,但今天由于个人身边的一些事,所以对做web和做游戏开发的区别做个记录,以下都是从网上搜索到的资料文章,感 ...

  9. MPP大规模并行处理架构详解

    面试官:说下你知道的MPP架构的计算引擎? 这个问题不少小伙伴在面试时都遇到过,因为对MPP这个概念了解较少,不少人都卡壳了,但是我们常用的大数据计算引擎有很多都是MPP架构的,像我们熟悉的Impal ...

  10. {"errcode":40017,"errmsg":"invalid button type hint: [I8nq_a0783sha1]"}

    在开发微信公众号 添加菜单时遇到问题 一直提示:{"errcode":40017,"errmsg":"invalid button type hint ...