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

思路:使用二分法,将list的中间节点作为根节点,然后分别处理list左半边及右半边,以此递归。

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) {
if(num.empty()) return NULL;
TreeNode* root = new TreeNode();
dfs(num,,num.size()-,root);
return root;
}
void dfs(vector<int> &num,int start, int end,TreeNode* treeNode)
{
int size = end-start+;
if(size == )
{
treeNode->val = num[start];
return;
}
int mid = size/ + start;
treeNode->val = num[mid];
treeNode->left = new TreeNode();
dfs(num,start,mid-,treeNode->left);
if(mid+<=end)
{
treeNode->right = new TreeNode();
dfs(num,mid+,end,treeNode->right);
}
}
};

108.Convert Sorted Array to Binary Search Tree(Array; Divide-and-Conquer, dfs)的更多相关文章

  1. 108. Convert Sorted Array to Binary Search Tree 109. Convert Sorted List to Binary Search Tree -- 将有序数组或有序链表转成平衡二叉排序树

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

  2. 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.每次更新的 ...

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

  4. 34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree

    Convert Sorted List to Binary Search Tree OJ: https://oj.leetcode.com/problems/convert-sorted-list-t ...

  5. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  6. Convert Sorted List to Binary Search Tree&&Convert Sorted Array to Binary Search Tree——暴力解法

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

  7. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  8. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

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

  10. Convert Sorted List to Binary Search Tree——将链表转换为平衡二叉搜索树 &&convert-sorted-array-to-binary-search-tree——将数列转换为bst

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

随机推荐

  1. postcss gulp 安装使用

    备注:    测试使用的是gulp 进行的编译 1. 项目初始化 npm init mkdir src touch app.css body{ display: flex; } 2. 安装(gulp ...

  2. 含锂电池的 PCBA 运输快递时如何包装?

    含锂电池的 PCBA 运输快递时如何包装? PCBA 和电池必须固定. PCBA 和电池必须独立包装. 独立包装的外壳必须为硬包装,防止运输中挤压导致短路. 电池电量在 80% 或以下.

  3. python编程规范系列--建议01~07

    本系列来自<编写高质量代码 改善python程序的91个建议>的读书笔记整理. 本书主要内容     1)容易被忽视的重要概念和常识,如代码的布局和编写函数的原则等:     2)编写py ...

  4. vue的路由初识01

    今天就做了一个vue-router的实例,(路由跳转,参数的传递[一个参数,多个参数])<!DOCTYPE html> <html> <head> <meta ...

  5. 常见企业IT支撑【8、端口回流问题】

    端口回流故障场景,常见于内网启用了Server服务器,使用出口路由设备的外网口NAT映射了一个公网地址,域内内网主机访问了这个公网地址,访问不通. 故障容易出现在办公网内的带有对外Server测试环境 ...

  6. Unit06: 状态管理-cookie

    Unit06: 状态管理-cookie web package web; import java.io.IOException; import java.io.PrintWriter; import ...

  7. cowboy的cookie和session的例子

    session插件需要下载https://github.com/chvanikoff/cowboy_session 如果session需要分布式存储,可以参考https://github.com/sp ...

  8. Ten Qualities of an Effective Team Player

    If you were choosing team members for a business team in your organization, who would the best team ...

  9. postman 请求 页面出现 Could not get any response 解决方法

    1.关闭掉证书 2.再关闭掉代理 3.去掉请求头的Content-Length字段 (或者一个个试请求头的值,看去掉那些就请求成功了) 后记: 网友 @ 重庆张晓祥 提供了个线索确实让我想起以前我从浏 ...

  10. Android软键盘弹出将底部栏顶上去并不会挤压界面

    界面需要,找到了一种不需要去设置android:windowSoftInputMode属性的解决keyboard和layout不适问题 有关设置android:windowSoftInputMode的 ...