1008. 先序遍历构造二叉树

 显示英文描述

 
  • 用户通过次数169
  • 用户尝试次数183
  • 通过次数171
  • 提交次数247
  • 题目难度Medium

返回与给定先序遍历 preorder 相匹配的二叉搜索树(binary search tree)的根结点。

(回想一下,二叉搜索树是二叉树的一种,其每个节点都满足以下规则,对于 node.left 的任何后代,值总 < node.val,而 node.right 的任何后代,值总 > node.val。此外,先序遍历首先显示节点的值,然后遍历 node.left,接着遍历 node.right。)

示例:

输入:[8,5,1,7,10,12]
输出:[8,5,10,1,7,null,12]

提示:

  1. 1 <= preorder.length <= 100
  2. 先序 preorder 中的值是不同的。
/**
* 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* dfs(vector<int> v,int start,int end){
if(start > end) return NULL;
TreeNode* p = new TreeNode(v[start]);
if(start == end) return p;
int cnt=start;
for(int i=start+;i <= end;i++){
if(v[i]<v[start])cnt = i;
else break;
}
p->left = dfs(v,start+,cnt);
p->right = dfs(v,cnt+,end);
return p;
} TreeNode* bstFromPreorder(vector<int>& preorder) {
return dfs(preorder,,preorder.size()-);
}
};

_我写的和大佬写的

/**
* 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* bstFromPreorder(vector<int>& a) {
int n = a.size();
if (n == ) return NULL;
TreeNode *it = new TreeNode(a[]);
vector<int> L, R;
for (auto x : a)
{
if (x < a[]) L.push_back(x);
if (x > a[]) R.push_back(x);
}
it->left = bstFromPreorder(L);
it->right = bstFromPreorder(R);
return it;
}
};

——这写法也太吊了吧,太美了!

Leetcode 1008. 先序遍历构造二叉树的更多相关文章

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

    Leetcode:1008. 先序遍历构造二叉树 Leetcode:1008. 先序遍历构造二叉树 思路 既然给了一个遍历结果让我们建树,那就是要需要前序中序建树咯~ 题目给的树是一颗BST树,说明中 ...

  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. java泛型的作用和好处

    转载于:https://blog.csdn.net/u012760183/article/details/52092692 之前参加面试的时候遇到面试官问泛型的作用,只是说了大概自己的理解, 在此转载 ...

  2. 火狐对 min-height 的支持

    代码: <!DOCTYPE html> <style> .com-center-banner { background: #f00; } .com-center-banner ...

  3. 6、nginx的反向代理及缓存功能

    nginx模块的应用 ngx_http_proxy_module  nginx 反向代理模块: http://nginx.org/en/docs/http/ngx_http_proxy_module. ...

  4. HDU 5724 Chess(SG函数+状态压缩)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=5724 题意: 现在有一个n*20的棋盘,上面有一些棋子,双方每次可以选择一个棋子把它移动到其右边第一 ...

  5. 2.在Jenkins中配置及执行 maven + selenium + testng项目

    1. 在Jenkins中配置Maven与Git 1)在系统管理>管理插件>可选插件 页面分别下载Git plugin 与 Maven Integration plugin插件,安装完成后再 ...

  6. tomcat中实现特定路径下的图片的url访问Tomcat配置图片保存路径,图片不保存在项目路径下

    使用Tomcat作为服务器的时候,如果不配置图片保存路径,将图片保存在项目路径下,那么再次打war包发布项目可能会造成图片的丢失,每次重启前将图片先保存再copy到服务器明显不方便,这时可以配置图片保 ...

  7. Oracle DBHelper 第二版

    public static class OracleDBHelper { public static OracleCommand cmd = null; public static OracleCon ...

  8. ECharts 使用总结

    1.去掉Echarts 图标上边框和右边框 option = { title: { text: '未来一周气温变化', subtext: '纯属虚构' }, grid: { show: 'true', ...

  9. Intellij Idea修改css文件即时更新生成效果

    用来Idea也有一段时间了,觉得还是有很多地方没有用到,今天遇到了一个问题,百度了解决方法,正好在这里做一个小记录 主要问题是我在idea的项目里面修改了css文件,然后运行web文件,发现并没有做到 ...

  10. 清华镜像方法更新python包

    来自:Jinlong_Xu cmd环境下执行: conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pk ...