- 浏览: 183482 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
给定一个排好序的数组,构造一颗平衡的二叉搜索树。我们可以每次都取数组的中间点作为当前子树的根节点,递归完成。代码如下:
给定一个排好序的数组,构造一颗平衡的二叉搜索树。我们可以每次都取数组的中间点作为当前子树的根节点,递归完成。代码如下:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode sortedArrayToBST(int[] nums) { if(nums == null || nums.length == 0) return null; int l = 0; int r = nums.length - 1; int m = l + (r - l) / 2; TreeNode root = new TreeNode(nums[m]); root.left = sortedArrayToBST(Arrays.copyOfRange(nums, 0, m)); root.right = sortedArrayToBST(Arrays.copyOfRange(nums, m + 1, nums.length)); return root; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
python python_leetcode题解之108_Convert_Sorted_Array_to_Binary_Search_Tree
js js_leetcode题解之108-convert-sorted-array-to-binary-search-tree.js
29. Convert Sorted Array to Binary Search Tree:将有序数组转换为高度平衡的二叉搜索树。 30. Convert Sorted List to Binary Search Tree:将有序链表转换为高度平衡的二叉搜索树。 31. Binary Tree Maximum ...
- Convert Sorted Array to Binary Search Tree(将有序数组转换为二叉搜索树) 7. Bit Manipulation(位运算): 位运算是对整数在内存中的二进制形式进行的运算。它是一种低级操作,但能高效地完成某些特定的任务...
Convert Sorted Array to Binary Search Tree string and search First Bad Version Dynamic Programing *** Climbing Stairs Set Matrix Zeroes API System.arrayCopy 刷题顺序 TOP100 其中算法,主要是以下几种: ...
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...
2. **将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree)**:利用有序数组的特点,通过递归构建平衡的二叉搜索树。 ### 动态规划 动态规划是解决复杂问题的常用方法,主要通过将问题分解...
array 239. Sliding Window Maximum 255. Verify Preorder Sequence in Binary Search Tree 907. Sum of Subarray Minimums 二叉搜索树 99. Recover Binary Search Tree 109. Convert Sorted List to Binary Search ...
29. Convert Sorted Array to Balanced Binary Search Tree:将有序数组转换成平衡的二叉搜索树。 30. Convert Sorted List to Balanced Binary Search Tree:将有序链表转换成平衡的二叉搜索树。 31. Binary Tree ...
- **Convert Sorted List/Array to Binary Search Tree**:将有序列表或数组转换为二叉搜索树。 - **Path Sum II**:寻找所有从根节点到叶节点的路径,其路径和等于给定的目标值。 - **Flatten Binary Tree to ...
leetcode添加元素使和等于 LeetCode leetcode 指针类型题目 1. 链表 函数参数传入的链表都没有链表的头,从第一个有数据的节点开始 函数返回一个链表,这个链表也没有头 2. 内存分配 二维数组内存分配 ...
**1.9 Convert Sorted List to Binary Search Tree (109)** - **问题描述**:给定一个链表,将其转换为平衡二叉搜索树。 - **解题思路**: - 先找到链表的中间节点作为根节点。 - 使用递归方式处理左子树和右子树...
- **整数转换(Convert Integer A to Integer B)**: 将一个整数转换为另一个整数。 - **阶乘末尾0的个数(Factorial Trailing Zeroes)**: 计算一个整数阶乘结果末尾有多少个零。 - **唯一二叉搜索树(Unique Binary ...
PEP 529: Change Windows filesystem encoding to UTF-8 PEP 528: Change Windows console encoding to UTF-8 PEP 520: Preserving Class Attribute Definition Order PEP 468: Preserving Keyword Argument ...