177 - 排序数组转为二叉树
2017-04-06 09:16
197 查看
4.6
清明前做了一半的题目,今天做出来了。
学到几个小的点,计算log2 的时候 应该是 Math.log(n)/ Math.log(double(2)),还是很麻烦的。
这道题的想法就是,先算出最小的树高,然后就可以找到根节点的位置了。
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param A: an integer array
* @return: a tree node
*/
public TreeNode sortedArrayToBST(int[] A) {
//TreeNode root = new TreeNode();
if(A.length == 0){
return null;
}
int low = 0;
int height = A.length -1;
return sortedArrayToBST1(A,low,height);
}
public TreeNode sortedArrayToBST1(int[] A,int low,int height) {
//TreeNode root = new TreeNode();
if(low > height){// write your code here
return null;
}
//TreeNode root = new TreeNode();
if(height == low){
TreeNode root = new TreeNode(A[low]);
return root;
}
//System.out.println("height:" + height +" low:" + low);
int level = (int)(Math.log(height - low +1)/Math.log((double)2));
//System.out.println("level:" + level);//高度减一
int flag = (int)Math.pow(2, level)-1;
//System.out.println("flag:" + flag);
TreeNode root = new TreeNode(A[low + flag]);
// System.out.println("root.val:" + root.val);
root.left = sortedArrayToBST1(A,low,low + flag-1);
root.right = sortedArrayToBST1(A,low + flag+1,height);//根节点的位置
return root;
}
}
清明前做了一半的题目,今天做出来了。
学到几个小的点,计算log2 的时候 应该是 Math.log(n)/ Math.log(double(2)),还是很麻烦的。
这道题的想法就是,先算出最小的树高,然后就可以找到根节点的位置了。
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param A: an integer array
* @return: a tree node
*/
public TreeNode sortedArrayToBST(int[] A) {
//TreeNode root = new TreeNode();
if(A.length == 0){
return null;
}
int low = 0;
int height = A.length -1;
return sortedArrayToBST1(A,low,height);
}
public TreeNode sortedArrayToBST1(int[] A,int low,int height) {
//TreeNode root = new TreeNode();
if(low > height){// write your code here
return null;
}
//TreeNode root = new TreeNode();
if(height == low){
TreeNode root = new TreeNode(A[low]);
return root;
}
//System.out.println("height:" + height +" low:" + low);
int level = (int)(Math.log(height - low +1)/Math.log((double)2));
//System.out.println("level:" + level);//高度减一
int flag = (int)Math.pow(2, level)-1;
//System.out.println("flag:" + flag);
TreeNode root = new TreeNode(A[low + flag]);
// System.out.println("root.val:" + root.val);
root.left = sortedArrayToBST1(A,low,low + flag-1);
root.right = sortedArrayToBST1(A,low + flag+1,height);//根节点的位置
return root;
}
}
相关文章推荐
- 根据一个已排序数组构造一棵二叉树,要求树的高度最少
- 将排序数组转为平衡二叉查找树
- [LintCode 177] 把排序数组转换为高度最小的二叉搜索树(Python)
- 数组按照拼音首字母排序后转为字符串
- 怎样编写一个程序,把一个有序整数数组放到二叉树中? 编写实现链表排序的一种算法。说明为什么你会选择用这样的方法?
- lintcode-177-把排序数组转换为高度最小的二叉搜索树
- 把排序数组转化为高度最小的二叉树
- 【算法练习】二叉树方法对数组进行排序
- lintcode-把排序数组转换为高度最小的二叉搜索树-177
- 练习LINQ:同时操作二维和一维数组,把二维数组转为一维,计算一维和二维值,字符串组合,计算数组和,数组筛选,排序
- LintCode 177-把排序数组转换为高度最小的二叉搜索树
- 二叉树结构字符串转为数组控制台输出二叉树
- 把一个有序的数组转为二叉树存储
- 把顺序数组转为高度最小的二叉树(算法)
- Lintcode 177.把排序数组转换为高度最小的二叉搜索树
- 二叉树:排序数组转换成平衡二叉搜索树
- 177.把排序数组转换为高度最小的二叉搜索树
- lintCode(177)——把排序数组转换为高度最小的二叉搜索树
- C/C++面试题(三) 推断二叉树、快速排序递归实现、递归判断数组递增
- C/C++面试题(三) 推断二叉树、快速排序递归实现、递归判断数组递增