[LC] 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.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
Given the sorted array: [-10,-3,0,5,9],
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
0
/ \
-3 9
/ /
-10 5
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
if (nums == null || nums.length == 0) {
return null;
}
return buildBST(0, nums.length - 1, nums);
} private TreeNode buildBST(int start, int end, int[] nums) {
if (start > end) {
return null;
}
int mid = start + (end - start) / 2;
TreeNode cur = new TreeNode(nums[mid]);
cur.left = buildBST(start, mid - 1, nums);
cur.right = buildBST(mid + 1, end, nums);
return cur;
}
}
[LC] 108. Convert Sorted Array to Binary Search Tree的更多相关文章
- 37. leetcode 108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...
- [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 ...
- 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 ...
- 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 ...
- 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.每次更新的 ...
- [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 ...
- 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. 题目 ...
- Leetcode No.108 Convert Sorted Array to Binary Search Tree(c++实现)
1. 题目 1.1 英文题目 Given an integer array nums where the elements are sorted in ascending order, convert ...
- [LeetCode&Python] Problem 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 ...
随机推荐
- Cookie API和记录上次来访时间
1.什么是Cookie? Cookie是一种会话技术,用千将会话过程中的数据保存到用户的浏览器中,从而使浏览器和服务器可以更好地进行数据交互. 在现实生活中,当顾客在购物时,商城经常会赠送顾客一张会员 ...
- 正则表达式模式修正符 比如/esi
正则表达式模式修正符 比如/esi 作者: 字体:[增加 减小] 类型:转载 下面列出了当前在 PCRE 中可能使用的修正符.括号中是这些修正符的内部 PCRE 名.修正符中的空格和换行被忽略,其它字 ...
- Linq------连表查询
1 List<Student> list = new List<Student>() { ,sex="男"}, ,sex="男"}, , ...
- 深入JVM(一)JVM指令手册
本文按照如下思维导图组织 1. 栈和局部变量操作 1.1 将常量压入栈的指令 aconst_null 将null对象引用压入栈iconst_m1 将int类型常量-1压入栈iconst_0 将int类 ...
- Python—程序设计:抽象工厂模式
抽象工厂模式 内容:定义一个工厂类接口,让工厂子类来创建一系列相关或相互依赖的对象. 例:生产一部手机,需要手机壳.CPU.操作系统三类对象进行组装,其中每类对象都有不同的种类.对每个具体工厂,分别生 ...
- Miller_Rabin素性测试
1. 为什么需要素性测试? 我们其实已经知道有一些判断素数的方法,比如: 遍历测试:待测试数n与2,3,...√n做除法判断余数是否为零,如果没有任何一个数可以整除n,则说明n为素数 Wilson定理 ...
- MVC——再探MVC——增删查改
MVC 是我大学学的比较弱的,甚至不懂原理.(那时候都在准备蓝桥杯 软件杯比赛.) 在重新学 肯定要学MVC 现在知道了为什么叫MVC了 MVC是怎么工作的 MVC 是一个设计模式 控制器(Cont ...
- 将hello程序作为驱动程序编译进系统内核
0x00开始 恩,可能是我比较愚钝,一个内核编译搞了一天,各种问题,各种bug,几度无奈,也是因为我突发奇想,并没有按照原来的那种操作,我直接把helloworld程序放到内核模块中编译成了一个驱动程 ...
- 选择排序&冒泡排序
//直接选择排序 #include<stdio.h> void SelectionSort(int arr[],int len) { int i,j; int k,min; int tem ...
- bfs--奇怪的电梯P1135
计院有一个bug电梯,可能是hyk造的,很多bug,电梯只有两个按钮,“上”和“下”,电梯每层都可以停,每层都有一个数字Ki(0<=Ki<=n),当你在一层楼,你按“上”键会到1+K1层, ...