数组——将排序数组转换为平衡二叉搜索树
来源:互联网 发布:武汉网络诈骗最新消息 编辑:程序博客网 时间:2024/04/30 23:39
题目描述:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
和有序链表化为BST方法一样,递归求解;
注:中间必须要两个中间结点的后一个!
public class Solution { public TreeNode sortedArrayToBST(int[] num) { if(num == null||num.length == 0) return null; return helper(num,0,num.length-1); } public TreeNode helper(int []num,int low ,int high) { if(low>high) return null; int center=low+(high-low)/2; //若数组元素个数为偶数,则中间结点选取两个中的后一个。 if((high-low)%2==1) center++; TreeNode node=new TreeNode(num[center]); node.left=helper(num,low,center-1); node.right=helper(num,center+1,high); return node; }}
0 0
- 数组——将排序数组转换为平衡二叉搜索树
- 将有序数组转换为平衡二叉搜索树
- 将有序数组转换为平衡二叉搜索树
- 有序数组转换为平衡二叉搜索树
- 有序数组转换为平衡二叉树
- Convert Sorted Array to Binary Search Tree 将排序的数组转换为二叉搜索树
- 将排序数组转换为高度最小的二叉搜索树
- leetcode 将已排序的 数组/链表 转换为二叉搜索树(BST),Python实现
- lintCode(177)——把排序数组转换为高度最小的二叉搜索树
- lintcode——把排序数组转换为高度最小的二叉搜索树
- 有序数组转换为平衡二叉搜索树(Convert Sorted Array to Binary Search Tree)
- 108. Convert Sorted Array to Binary Search Tree 有序数组转换为平衡二叉搜索树
- 将有序单链表转换为平衡的二叉搜索树
- 将升序数组转化为平衡二叉树
- 将排序数组转为平衡二叉查找树
- 有序数组转换为平衡二叉树(BST)
- LintCode算法题解——奇偶分割数组、二进制中1个数、反转整数、加一、排序数组转换为高度最小的二叉搜索树、二进制求和
- LintCode SortedArrayToBST 把排序数组转换为高度最小的二叉搜索树
- 项目上线(一,加密)
- 汇编学习(一)8086汇编拾遗(1)
- 网络攻防平台第三关
- hive 中 union all ---Top level UNION is not supported currently 错误
- 同一天内的 某停车场进场出场的费用计算收取
- 数组——将排序数组转换为平衡二叉搜索树
- 一个程序员的进化史2
- Unity3d 顶点和片段着色器学习笔记
- 项目上线流程(二)
- iOS 各类宏定义的作用
- 一些面试题
- 谈谈C++中的swap函数
- 线程同步
- 15.8.2 泛型数组