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 ...
随机推荐
- go语言-变量与常量
变量 一.变量注意事项 变量名首字母大写,可以被其他包访问调用(公有),变量名首字母小写,其他包不能访问和调用(私有) 在同一个域里一个变量只能定义一次,不可重复定义 二.变量的声明的种方式 1.先声 ...
- Codeforces Global Round 2 E. Pavel and Triangles(思维+DP)
题目链接:https://codeforces.com/contest/1119/problem/E 题意:有n种长度的棍子,有a_i根2^i长度的棍子,问最多可以组成多少个三角形 题解:dp[i]表 ...
- Python3连接MySQL数据库实战
Python3连接MySQL数据库实战 第三方库 :pymysql 数据库连接 def connect(): try: #建立数据库连接,从左至右参数依次为 # ip地址 我用的是云端数据库 如果为本 ...
- git的优点跟缺点?
优点: 1.分布式开发时,可以git clone克隆一个本地版本,然后在本地进行操作提交,本地可以完成一个完整的版本控制.在发布的时 候,使用git push来推送到远程即可. 2.git分支的本质是 ...
- easyui--权限管理
1.权限目的: 是为了让不同的用户可以操作系统中不同资源 直接点说就是不同的用户可以操作不同的菜单 核心:实现菜单权限的核心思想就是控制用户登录后台所传递的menuId(与树形菜单分类列段 ...
- Vue.js最佳实践(五招助你成为vuejs大师)
转自https://www.jb51.net/article/139448.htm 本文面向对象是有一定Vue.js编程经验的开发者.如果有人需要Vue.js入门系列的文章可以在评论区告诉我,有空就给 ...
- vim基本配置
#set nocompatible # 打开语法高亮 syntax on # 在底部显示当前模式 set showmode # 命令模式下显示键入的指令 set showcmd # 支持使用鼠标 se ...
- PHP yield占用内存测试
function com($start) { $tmp = []; for($i=0; $i<300000; $i++){ $tmp[] = $i; } $end = memory_get_us ...
- [MUTC2013]idiots
嘟嘟嘟 首先\(O(n ^ 2)\)大家都会,枚举最长边,然后找两条短边满足两边之大于第三边即可. 然后估计就没法优化了. 正难则反,如果枚举的两条短边小于等于第三边会怎么样呢?发现\(a_i \le ...
- python常用函数1
map()函数 map()是python 内置 的高届函数 ,接收一个函数 f 和一个list,并通过把函数 f 依次作用在list的每个元素上,得到一个新的 list 并返回. 比如,对于l ...