import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; /**
*
* Source : https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
*
* Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
*
*/
public class ConvertSortedArray { /**
* 把一个已排序的数组转化我一颗高度平衡二叉搜索树,即AVL树,具有以下性质:
* 它是一棵空树或者左右两个子树的高度差不超过1,并且左右子树也都是AVL树
*
* 因为是已排序的数组,只需要找到数组中间位置的值作为根节点,左边的为左子树,右边的为右子树,递归构造即可
*
* @param arr
* @param left
* @param right
* @return
*/
public TreeNode convert (int[] arr, int left, int right) {
if (left > right) {
return null;
}
int mid = (left + right) / 2;
TreeNode root = new TreeNode(arr[mid]);
root.leftChild = convert(arr, left, mid-1);
root.rightChild = convert(arr, mid+1, right);
return root;
} public void binarySearchTreeToArray (TreeNode root, List<Character> chs) {
if (root == null) {
chs.add('#');
return;
}
List<TreeNode> list = new ArrayList<TreeNode>();
int head = 0;
int tail = 0;
list.add(root);
chs.add((char) (root.value + '0'));
tail ++;
TreeNode temp = null; while (head < tail) {
temp = list.get(head);
if (temp.leftChild != null) {
list.add(temp.leftChild);
chs.add((char) (temp.leftChild.value + '0'));
tail ++;
} else {
chs.add('#');
}
if (temp.rightChild != null) {
list.add(temp.rightChild);
chs.add((char)(temp.rightChild.value + '0'));
tail ++;
} else {
chs.add('#');
}
head ++;
} //去除最后不必要的
for (int i = chs.size()-1; i > 0; i--) {
if (chs.get(i) != '#') {
break;
}
chs.remove(i);
}
}
private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value; public TreeNode(int value) {
this.value = value;
} public TreeNode() { }
} public static void main(String[] args) {
ConvertSortedArray convertSortedArray = new ConvertSortedArray();
int[] arr = new int[]{1,2,3,4,5};
List<Character> chs = new ArrayList<Character>();
convertSortedArray.binarySearchTreeToArray(convertSortedArray.convert(arr, 0, arr.length-1), chs);
System.out.println(Arrays.toString(chs.toArray(new Character[chs.size()])));
}
}

leetcode — convert-sorted-array-to-binary-search-tree的更多相关文章

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

  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] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  4. LeetCode——Convert Sorted Array to Binary Search Tree

    Description: Given an array where elements are sorted in ascending order, convert it to a height bal ...

  5. LeetCode - Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  6. [leetcode]Convert Sorted Array to Binary Search Tree @ Python

    原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉 ...

  7. LeetCode Convert Sorted Array to Binary Search Tree(数据结构)

    题意: 将一个有序的数组建成一棵平衡的BST树. 思路: 因为数组已经有序,每次可以从中点开始建根,再递归下去分别处理左/右子树. /** * Definition for a binary tree ...

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

  9. LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14

    108. 将有序数组转换为二叉搜索树 108. Convert Sorted Array to Binary Search Tree 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索 ...

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

随机推荐

  1. kodexplorer免费网盘安装教程

    KodExplorer是什么? KodExplorer可道云,原名芒果云,是基于Web技术的私有云和在线文件管理系统. 用户只需通过简单环境搭建,即可使用KodExplorer快速完成私有云/私有网盘 ...

  2. diy 滚动条 样式 ---- 核心代码

    参考自 : https://blog.csdn.net/qq_38881495/article/details/83689721 .chapter_data position relative wid ...

  3. oracle 创建的表为什么在table里没有,但是可以查出来

    有两种的可能: 1这个表在其他用户下创建的,当前用户没有权限访问,此表不在属于当前用户 2查询时写的表名,并不是真正意义的表名,可能指向其他用户,或者就不是这个表

  4. Linux了解知识点

    Linux知识点   1.linux系统内核最早由芬兰大学生linus Torvalds开发. 2.Linux主要用于服务器端和嵌入式两个领域. 3.Linux的特点:开放性.多用户.多任务.良好的用 ...

  5. cadence PCB板级设计

    总结PCB板框设计,定位孔的放置,以及布线区域和元件放置区域的放置,最重要的是层叠结构的设计.

  6. [BlueZ] 2、使用bluetoothctl搜索、连接、配对、读写、使能notify蓝牙低功耗设备

    星期三, 05. 九月 2018 02:03上午 - beautifulzzzz 1.前言 上一篇讲了如何编译安装BlueZ-5,本篇主要在于玩BlueZ,用命令行去操作BLE设备: [BlueZ] ...

  7. Java实现桶排序和基数排序

    桶排序代码: import java.util.Arrays; /** * 桶排序 * 工作的原理是将数组分到有限数量的桶里 * 每个桶再分别排序(有可能再使用别的排序算法或是以递归方式继续使用桶排序 ...

  8. #Java学习之路——基础阶段(第十篇)

    我的学习阶段是跟着CZBK黑马的双源课程,学习目标以及博客是为了审查自己的学习情况,毕竟看一遍,敲一遍,和自己归纳总结一遍有着很大的区别,在此期间我会参杂Java疯狂讲义(第四版)里面的内容. 前言: ...

  9. [Swift]LeetCode272. 最近的二分搜索树的值 II $ Closest Binary Search Tree Value II

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  10. [Swift]LeetCode437. 路径总和 III | Path Sum III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...