package Level2;
import Utility.TreeNode;
/**
* Convert Sorted Array to Binary Search Tree
*
* Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
*/
public class S108 {
public static void main(String[] args) {
int[] num = {1,3};
TreeNode n = sortedArrayToBST(num);
n.print();
}
public static TreeNode sortedArrayToBST(int[] num) {
return sortedArrayToBSTRec(num, 0, num.length-1);
}
// 二分查找节点值,并递归创建节点
private static TreeNode sortedArrayToBSTRec(int[] num, int low, int high){
if(low > high){
return null;
}
int mid = low + (high-low)/2; // 找到中值
TreeNode root = new TreeNode(num[mid]); // 创建根节点
root.left = sortedArrayToBSTRec(num, low, mid-1); // 递归创建左子树和右子树
root.right = sortedArrayToBSTRec(num, mid+1, high);
return root;
}
}
分享到:
相关推荐
Merge Sorted Array 合并 排序 数组 leetcode
lru缓存leetcode leetcode 大批 41. First Missing Positive 广度优先搜索 773. Sliding Puzzle 864. Shortest Path to Get All Keys 深度优先搜索 996. Number of Squareful Arrays 拓扑排序 269. Alien Dictionary...
Convert Sorted Array to Binary Search Tree Convert Sorted List to Binary Search Tree LCA of BST Kth Smallest Element in a BST 二叉树的递归 Minimum Depth of Binary Tree Maximum Depth of Binary Tree ...
leetcode卡 leetcode exercises 3-5 solutions everyday. fighting~ TODO array Best Time to Buy and Sell Stock II Valid Sudoku linked list Palindrome linked list Linked List Cycle trees Convert Sorted ...
26 | [Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| [C++](./C++/remove-duplicates-from-sorted-array.cpp) [Python](./Python/remove-duplicates...
LeetCode Remove Duplicates from Sorted Array解决方案
The most common application of binary search is to find a specific value in a sorted list. To cast this in the frame of the guessing game (see Example below), realize that we are now guessing the ...
leetcode lintcode差异 Lintcode 解题思路记录 Table of Contents Linked List Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ascending order, convert it ...
leetcode添加元素使和等于 LeetCode leetcode 指针类型题目 1. 链表 函数参数传入的链表都没有链表的头,从第一个有数据的节点开始 函数返回一个链表,这个链表也没有头 2. 内存分配 二维数组内存分配 二维数组分配...
26.Remove_Duplicates_from_Sorted_Array删除有序数组中的重复项【LeetCode单题讲解系列
34.Find_First_and_Last_Position_of_Element_in_Sorted_Array在排序数组中
m排序数组 插入,查找和删除排序数组的实现安装$ npm install --save m-sorted-array用法var SortedArray = require ( 'm-sorted-array' ) ;var SortedArray = require ( './lib' ) ;var sortedArray = SortedArray ...
SortedArray 该程序包实现了SortedArray类型,该类型是始终对其内容进行排序的数组。 它支持所有标准的ES6 Array方法,并提供了与使元素保持排序顺序有关的其他方法。 它完全支持索引,枚举和length属性。 该...
lru cache leetcode Coding-Interview A repo for popular coding interview ...Leetcode. ...Sorted Array ...Sorted Array ...Binary Search Tree Value 二叉树查找/二叉树第K个 Kth Smallest Element In A
lru缓存leetcode LeetCode_Note leetcode 个人笔记 问题清单 [1_two_sum.cpp] [10_regular-expression-matching.cpp] [100_same-tree.cpp] [1001_sorted-merge-lcci.cpp] [101_对称树.cpp] ...[108_convert-sorted
Remove Duplicates from Sorted Array iii. Plus One iv. Pascal's Triangle v. Merge Sorted Array vi. Sum vii. Find Minimum in Rotated Sorted Array viii. Largest Rectangle in Histogram ix. Maximal ...
leetcode 分类 :person_running::person_running::person_running: 算法 :person_running::person_running::person_running: 实践&理论 :books: :ear: :television: Binary Search(二分查找) easy 69: 278: 35: ...
LeetCode Merge 2 Sorted Lists解决方案
颜色分类leetcode My Leetcode Problems Solutions Using javascript(ES6) 1 Two Sum 两数之和 5 Longest Palindromic Substring 最长回文子串 7 Reverse Integer 整数反转 9 Palindrome Number 回文数 11 Container...
leetcode LeetCode 这个库用于总结leetcode中遇到的习题 常用数据结构习题总结 1.线性表 解决进度 No. Describition mark 1 Remove Duplicates from Sorted Array 2 Remove Duplicates from Sorted Array II 3 ...