一、     称号

排序后的数组成二叉搜索树。

二、     分析

BST的中序遍历是一个sorted-array,再构造回去成一个BST,先将中间的元素作为根节点,这个节点的左右各自是左子树和右子树。如此递归地进行就可以。

/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *sortedArrayToBST(vector<int> &num) {
return addNode(num, 0, num.size()-1);
}
TreeNode *addNode(vector<int> &num, int start, int end){
if(start > end) return NULL; int mid = (start + end)/2;
TreeNode *root = new TreeNode(num[mid]);
root->left = addNode(num, start, mid-1);
root->right = addNode(num, mid+1, end);
return root;
}
};

版权声明:本文博主原创文章。博客,未经同意不得转载。

Leetcode:convert_sorted_array_to_binary_search_tree的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. spring mvc综合easyui点击上面菜单栏中的菜单项问题

    采用easyui的tree报错发生的背景后,会弹出一个窗口,有一个问题是,当你点击顶部   解决方案,如下面(运用easyui1.36): /home/cyz/workspace/hb_manager ...

  2. js jquery版本号 金额千分之一转换功能(非规范,高效率)

    没想到js将没有 金额千分之一格处理器类型(例子:1,234.01 这种格公式).互联网搜索圈,我们使用的是常规方式.常规效率受宠若惊啊.和资源密集型,速度慢(虽然处理起来会很直观). 因此专门写一个 ...

  3. CSDN 夏令营课程 项目分析

    主题如以下: 正确改动后的程序: #include <iostream.h> //using namespace std; class BASE { char c; public: BAS ...

  4. async And await异步编程活用基础

    原文:async And await异步编程活用基础 好久没写博客了,时隔5个月,奉上一篇精心准备的文章,希望大家能有所收获,对async 和 await 的理解有更深一层的理解. async 和 a ...

  5. Stack-based buffer overflow in acdb audio driver (CVE-2013-2597)

    /* 本文章由 莫灰灰 编写,转载请注明出处. 作者:莫灰灰    邮箱: minzhenfei@163.com */ 1. 漏洞描写叙述 音频驱动acdb提供了一个ioctl的系统接口让应用层调用, ...

  6. php 两个文件之间的相对路径的计算方法

    php 两个文件之间的相对路径的计算方法 比如: 文件A 的路径是 /home/web/lib/img/cache.php 文件B的路径是 /home/web/api/img/show.php 那么. ...

  7. canvas绘制百分比圆环进度条

    开发项目,PM会跟踪项目进度:完成某个事情,也可以设置一个完成的进度. 这里用canvas绘制一个简单百分比圆环进度条. 看下效果: 1. 动画方式   2. 静默方式   // 贴上代码,仅供参考 ...

  8. 源代码版本控制工具TortoiseSVN,AnkhSVN最新版本下载地址

    TortoiseSVN http://tortoisesvn.net/downloads.html 页面下部有中文语言补丁 AnkhSVN https://ankhsvn.open.collab.ne ...

  9. Codeforces 12D Ball 树形阵列模拟3排序元素

    主题链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...

  10. React-Native入门

    React-Native入门指导之iOS篇 React-Native 入门指导系列教程目录 一.准备工作 (已完成) 二.项目介绍与调试 三.CSS样式与Flex布局 四.常用UI控件的使用 五.JS ...