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

解题思路:

1. 找到数组的中间节点将其置为根节点

2. 左边的即为左子树

3. 右边的即为右子树

4. 递归求解

/**
* 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* sortBST(vector<int>& nums, int begin, int end){
if(begin > end)
return 0;
int rootIndex = begin+(end-begin)/2; //更简单的应该是 (begin+end)/2
TreeNode* root = new TreeNode(nums[rootIndex]);
root->left = sortBST(nums, begin, rootIndex-1);
root->right = sortBST(nums, rootIndex+1, end);
return root;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
return sortBST(nums, 0, nums.size()-1);
}
};

  

LeetCode 108: Convert Sorted Array to Binary Search Tree DFS求解的更多相关文章

  1. 37. leetcode 108. Convert Sorted Array to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...

  2. [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)

    108. Convert Sorted Array to Binary Search Tree 描述 Given an array where elements are sorted in ascen ...

  3. LeetCode 108. Convert Sorted Array to Binary Search Tree (将有序数组转换成BST)

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

  4. leetcode 108. Convert Sorted Array to Binary Search Tree 、109. Convert Sorted List to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree 这个题使用二分查找,主要要注意边界条件. 如果left > right,就返回NULL.每次更新的 ...

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

  6. 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. 题目 ...

  7. Java for 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. 解题 ...

  8. leetcode 108 Convert Sorted Array to Binary Search Tree ----- java

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

  9. Leetcode#108 Convert Sorted Array to Binary Search Tree

    原题地址 对于已排序数组,二分法递归构造BST 代码: TreeNode *buildBST(vector<int> &num, int i, int j) { if (i > ...

随机推荐

  1. MyEclipse下安装MyBatis Generator代码反向生成工具

    一.离线方式: 在http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/下载 featur ...

  2. 1709: [Usaco2007 Oct]Super Paintball超级弹珠

    1709: [Usaco2007 Oct]Super Paintball超级弹珠 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 339  Solved:  ...

  3. Python第五章__模块介绍,常用内置模块

    Python第五章__模块介绍,常用内置模块 欢迎加入Linux_Python学习群  群号:478616847 目录: 模块与导入介绍 包的介绍 time &datetime模块 rando ...

  4. java之JDK的环境变量配置

    JDK是什么? JDK是整个java开发的核心,它包含了JAVA的运行环境,JAVA工具和JAVA基础的类库. JDK包含的基本组件包括 java –--------> 运行编译后的java程序 ...

  5. RobotFramework中解析中文报错UnicodeDecodeError

    在RobotFramework中解析一段包含中文的字符串时遇到下面的报错: FAIL : UnicodeDecodeError: 'ascii' codec can't decode byte 0xe ...

  6. spring项目中dubbo相关的配置文件出现红叉的问题

    近来在eclipse中导入了一个web项目,但是发现项目上有红色的叉号. 原来是spring中关于dubbo的配置文件报错了. Multiple annotations found at this l ...

  7. JS清除DIV的选中状态

    var clearSlct = "getSelection" in window ? function () { window.getSelection().removeAllRa ...

  8. 读书笔记 effective c++ Item 37 永远不要重新定义继承而来的函数默认参数值

    从一开始就让我们简化这次的讨论.你有两类你能够继承的函数:虚函数和非虚函数.然而,重新定义一个非虚函数总是错误的(Item 36),所以我们可以安全的把这个条款的讨论限定在继承带默认参数值的虚函数上. ...

  9. ruby安装sass报错解决办法

    ERROR: Could not find a valid gem 'sass' (>= 0), here is why: Unable to download data from http:/ ...

  10. C++ 网络爬虫实现

    最近有个概念吵得很火,网络爬虫,但是基本都是用什么python或者JAVA写,貌似很少看到用c++写的,我在网上找了一个,看到其实还是很简单的算法 算法讲解:1.遍历资源网站 2.获取html信息   ...