Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced 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) { TreeNode *root=buildTree(num,,num.size()-);
return root;
} TreeNode *buildTree(vector<int> &num,int l,int r)
{
if(l>r) return NULL; int mid=(l+r)/; TreeNode *root=new TreeNode(num[mid]);
root->left=buildTree(num,l,mid-);
root->right=buildTree(num,mid+,r); return root;
}
};

【leetcode】Convert Sorted Array to Binary Search Tree的更多相关文章

  1. 【leetcode】Convert Sorted Array to Binary Search Tree (easy)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序 ...

  2. 【题解】【BST】【Leetcode】Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST.思路: ...

  3. 【leetcode】Convert Sorted List to Binary Search Tree

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  4. 【Leetcode】【Medium】Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题 ...

  5. 【树】Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  6. 【leetcode】Convert Sorted List to Binary Search Tree (middle)

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  7. 【LeetCode OJ】Convert Sorted Array to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...

  8. [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  9. [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

随机推荐

  1. Semantic ui 学习笔记 持续更新

    这个semantic 更新版本好快~ 首先是代码的标识<code></code> 具体样式就是红框这样的 圈起来代码感觉不错 不过要在semantic.css里在加上如下样式~ ...

  2. 安装 SQL server 2008 R2

    操作系统:WIN7 问题: The Windows Installer Service could not be accessed. This can occur if the Windows Ins ...

  3. [webgrid] – Ajax – (Reloading a Razor WebGrid after Ajax calls using a partial view)

    Reloading a Razor WebGrid after Ajax calls using a partial view If you are using Razor and MVC you p ...

  4. 缓存 HttpContext.Current.Cache和HttpRuntime.Cache的区别

    先看MSDN上的解释: HttpContext.Current.Cache:为当前 HTTP 请求获取Cache对象. HttpRuntime.Cache:获取当前应用程序的Cache.  我们再用. ...

  5. MYSQL调优

    4核8G内存配置文件 explain SQL 查看SQL索引使用情况. my.cnf skip-external-locking skip-name-resolve back_log= key_buf ...

  6. 看见了就转来了, 涉及到UBOOT 地址的一个问题.

    addr = (_bss_end + (PAGE_SIZE - 1)) & ~(PAGE_SIZE - 1);什么意思? 这是UBOOT 中的一个分配视频帧缓冲区地址的函数,我想问的是:加一个 ...

  7. 黄永成-thinkphp讲解-个人博客讲解26集

    如果是要导入 前后台分组都公用的 第三方类库, 就将类库(*.class.php文件) 放在 App下的Class文件夹中. 导入方法是: import('Class.Image', APP_PATH ...

  8. Emacs配置文件

    ;;tab and space;;when true,emacs use mixture of tab and space to archieve(setq-default indent-tabs-m ...

  9. java读写excel文件

    近期处理的数据规模比较大,正好又是统计合并的事情,想着借助excel就可以完成了,然后就了解了下java读取excel的事情. 读取的文件主要分两类:xls文件.xlsx文件.xls文件的相关操作用的 ...

  10. jQuery1.9.1源码分析--数据缓存Data模块

    jQuery1.9.1源码分析--数据缓存Data模块 阅读目录 jQuery API中Data的基本使用方法介绍 jQuery.acceptData(elem)源码分析 jQuery.data(el ...