Convert Sorted Array to Binary Search Tree
来源:互联网 发布:ipsec提供网络层 编辑:程序博客网 时间:2024/05/06 22:58
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode sortedArrayToBST(int[] num) { TreeNode root=null; if(num==null || num.length==0) return root; root=convert(root,0,num.length-1,num); return root; } public TreeNode convert(TreeNode node ,int left,int right,int []num){ if(left>right){ return null; } int mid=left+(right-left)/2; node=new TreeNode(num[mid]); node.left=convert(node.left,left,mid-1,num); node.right=convert(node.right,mid+1,right,num); return node; }}
0 0
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- ubuntu下安装中文输入法
- HDU - 1372 Knight Moves ( 简单BFS + floodfill )
- 买的Micromax Q7的优雅配饰
- scala学习二 scala中pojo的定义
- 一个java生成与使用WebService的简单例子
- Convert Sorted Array to Binary Search Tree
- MS-SQL Server字符串处理函数大全
- 了解Java开发的好处
- ios7和ios6适配中使用到的方法!!!
- Java 常用字符串函数
- VHDL入门
- 线程间通信的一种方式
- Ubuntu 12.04: How to enable root login
- Android MediaPlayer各个状态转换