108. Convert Sorted Array to Binary Search Tree

Easy

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的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. 【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 ...

  4. 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 ...

  5. 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 ...

  6. Convert Sorted Array to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  7. 37. leetcode 108. Convert Sorted Array to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 深入剖析TOMCAT

    理解tomcat之搭建简易http服务器 做过java web的同学都对tomcat非常熟悉.我们在使用tomcat带来的便利的同时,是否想过tomcat是如何工作的呢?tomcat本质是一个http ...

  2. [bzoj 2693] jzptab & [bzoj 2154] Crash的数字表格 (莫比乌斯反演)

    题目描述 TTT组数据,给出NNN,MMM,求∑x=1N∑y=1Mlim(x,y)\sum_{x=1}^N\sum_{y=1}^M lim(x,y)\newlinex=1∑N​y=1∑M​lim(x, ...

  3. linux其他

    1.安装上传下载指令 sz/rz yum install -y lrzsz 2.flask+gunicorn 代码更新升级部署 ps -ef | grep gunicorn 获取master进程 ki ...

  4. 简要概述java内存模型,以及volatile关键字

    如果我们要想深入了解Java并发编程,就要先理解好Java内存模型.Java内存模型定义了多线程之间共享变量的可见性以及如何在需要的时候对共享变量进行同步.原始的Java内存模型效率并不是很理想,因此 ...

  5. 012_Python3 斐波纳契数列 + end 关键字

    1.个斐波纳契数列. #!/usr/bin/python3   # Fibonacci series: 斐波纳契数列 # 两个元素的总和确定了下一个数 a, b = 0, 1 while b < ...

  6. 067_查看 KVM 虚拟机中的网卡信息(不需要进入启动或进入虚拟机)

    #!/bin/bash #该脚本使用 guestmount 工具,可以将虚拟机的磁盘系统挂载到真实机文件系统中#Centos7.2 中安装 libguestfs-tools-c 可以获得 guestm ...

  7. js中 0.1+0.2 !== 0.3

    1. 存储原理: 在计算机中数字无论是定点数还是浮点数都是以多位二进制的方式进行存储的.事实上不仅仅是 Javascript,在很多语言中 0.1 + 0.2 都会得到 0.3000000000000 ...

  8. [other] Div

    https://www.luogu.org/problemnew/show/U16765 解法一 随机输出一组合法解. 复杂度 O(1) 预计得分 10~??? 解法二 看完题目基本能想到大力贪心,通 ...

  9. [转]C++ 类中的static成员的初始化和特点

    在C++的类中有些成员变量初始化和一般数据类型的成员变量有所不同.以下测试编译环境为: ➜ g++ -v Using built-in specs. COLLECT_GCC=g++ Target: x ...

  10. yy

    sudo rm -rf /var/cache/apt/archives/python-catkin-pkg-modules_0.4.12-1_all.deb sudo rm -rf /var/cach ...