LeetCode_108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
Given the sorted array: [-10,-3,0,5,9],
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
0
/ \
-3 9
/ /
-10 5
package leetcode.easy; /**
* Definition for a binary tree node. public class TreeNode { int val; TreeNode
* left; TreeNode right; TreeNode(int x) { val = x; } }
*/
public class ConvertSortedArrayToBinarySearchTree {
public TreeNode sortedArrayToBST(int[] nums) {
if (0 == nums.length) {
return null;
}
TreeNode root = new TreeNode(nums[nums.length / 2]);
root.left = sortedArrayToBST(java.util.Arrays.copyOfRange(nums, 0, nums.length / 2));
root.right = sortedArrayToBST(java.util.Arrays.copyOfRange(nums, nums.length / 2 + 1, nums.length));
return root;
} private static void transLevel(TreeNode root) {
java.util.LinkedList<TreeNode> queue = new java.util.LinkedList<>();
if (null == root) {
return;
} else {
System.out.print(root.val + " ");
queue.offer(root);
while (!queue.isEmpty()) {
root = queue.pop();
if (root.left != null) {
System.out.print(root.left.val + " ");
queue.offer(root.left);
}
if (root.right != null) {
System.out.print(root.right.val + " ");
queue.offer(root.right);
}
} // end of the while
} // end of the if
} @org.junit.Test
public void test() {
int[] nums = { -10, -3, 0, 5, 9 };
TreeNode root = sortedArrayToBST(nums);
transLevel(root);
}
}
LeetCode_108. Convert Sorted Array to Binary Search Tree的更多相关文章
- [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【leetcode】Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【LeetCode OJ】Convert Sorted Array to Binary Search Tree
Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...
- 34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree
Convert Sorted List to Binary Search Tree OJ: https://oj.leetcode.com/problems/convert-sorted-list-t ...
- LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree
LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...
- Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 37. leetcode 108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)
108. Convert Sorted Array to Binary Search Tree 描述 Given an array where elements are sorted in ascen ...
- LeetCode: Convert Sorted Array to Binary Search Tree 解题报告
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
随机推荐
- Android上执行python脚本-QPython
看书,发现android可以跑python. 尝试了一下. 首先需要在手机上安装python环境,通过安装apk实现,这个apk叫QPython,还有同类的比如SL4A. QPython的官网:htt ...
- Mac系统上,Firefox和Selenium不兼容的情况
解决办法,检查环境: Python 2.7.10 Firefox 46版本 Selenium 2.53.6 注意:将Firefox自动更新关闭,否则可能会出现自动升级以后无法执行Selenium用例的 ...
- 19 Jquery 属性
从 jQuery 1.6 开始, .prop()方法 方法返回 property 的值,而 .attr() 方法返回 attributes 的值. 例如, selectedIndex, tagName ...
- WCF之MSMQ消息队列
一.MSMQ简介 MSMQ(微软消息队列)是Windows操作系统中消息应用程序的基础,是用于创建分布式.松散连接的消息通讯应用程序的开发工具. MSMQ与XML Web Services和.Net ...
- 1. let与const
1.ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. var a = []; for (var i = 0;i<10;i++) ...
- Django REST framework+Vue 打造生鲜电商项目(笔记三)
(PS:转载自http://www.cnblogs.com/derek1184405959/p/8810591.html 有修改) 一.drf的过滤 (1)添加到app里面 INSTALLED_AP ...
- CSP模拟赛 number (二分+数位DP)
题面 给定整数m,km,km,k,求出最小和最大的正整数 nnn 使得 n+1,n+2,-,2nn+1,n+2,-,2nn+1,n+2,-,2n 中恰好有 mmm 个数 在二进制下恰好有 kkk 个 ...
- 三十九.NoSQL概述 部署Redis服务 、 部署LNMP+Redis
1. 搭建Redis服务器 在主机 192.168.4.50 上安装并启用 redis 服务 设置变量test,值为123 查看变量test的值 1.1 搭建redis服务器 1.1.1 安装re ...
- leetcode解题报告(23):Pascal's Triangle
描述 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...
- P1213 时钟
题目描述 考虑将如此安排在一个 3 x 3 行列中的九个时钟: 目标要找一个最小的移动顺序将所有的指针指向12点.下面原表格列出了9种不同的旋转指针的方法,每一种方法都叫一次移动.选择1到9号移动方法 ...