leetcode — convert-sorted-list-to-binary-search-tree
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Source : https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
*
*
* Given a singly linked list where elements are sorted in ascending order,
* convert it to a height balanced BST.
*/
public class ConvertSortedList {
/**
* 将一个有序链表转化为一棵AVL树
* 可以先把单向链表转化为有序数组,然后将数组转化为AVL树
* 或者使用双指针,快指针每次移动两个位置,慢指针每次移动一个位置,这样就能找到中间的节点
*
* 还有一种方法:
* 找到链表总个数total,一次递归如下:
* 递归查找左子树,每次递归的时候将head指向下一个节点
* 左子树递归完成之后,head指向了mid节点
* 构造root节点,当前head指向的就是mid节点,也就是root节点
* 递归构造右子树
* 构造根节点,并返回
*
* @param head
* @return
*/
public TreeNode convert (ListNode head) {
ListNode node = head;
int total = 0;
while (node != null) {
total++;
node = node.next;
}
return convert(new HeadHolder(head), 0, total-1);
}
/**
* 因为每次递归需要改变head的值,所以使用一个HeadHolder指向head,每次修改headHolder的指向
*
* @param head
* @param left
* @param right
* @return
*/
public TreeNode convert (HeadHolder head, int left, int right) {
if (left > right) {
return null;
}
int mid = (left + right) / 2;
TreeNode leftChild = convert(head, left, mid-1);
// 上面递归完成之后,head指向了mid位置的节点
TreeNode root = new TreeNode(head.head.value);
// head的下一个节点就是右子树的第一个节点
head.head = head.head.next;
TreeNode rightChild = convert(head, mid+1, right);
root.leftChild = leftChild;
root.rightChild = rightChild;
return root;
}
private class HeadHolder {
ListNode head;
public HeadHolder(ListNode head) {
this.head = head;
}
}
public ListNode createList (int[] arr) {
if (arr.length == 0) {
return null;
}
ListNode head = new ListNode();
head.value = arr[0];
ListNode pointer = head;
for (int i = 1; i < arr.length; i++) {
ListNode node = new ListNode();
node.value = arr[i];
pointer.next = node;
pointer = pointer.next;
}
return head;
}
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() {
}
}
private class ListNode {
ListNode next;
int value;
public ListNode(int value) {
this.value = value;
}
public ListNode() {
}
}
public static void main(String[] args) {
ConvertSortedList convertSortedList = new ConvertSortedList();
int[] arr = new int[]{1,2,3,4,5,6};
List<Character> chs = new ArrayList<Character>();
TreeNode root = convertSortedList.convert(convertSortedList.createList(arr));
convertSortedList.binarySearchTreeToArray(root, chs);
System.out.println(Arrays.toString(chs.toArray(new Character[chs.size()])));
}
}
leetcode — convert-sorted-list-to-binary-search-tree的更多相关文章
- 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 ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [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. 这道 ...
- leetcode -- Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- LeetCode: Convert Sorted List to Binary Search Tree 解题报告
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...
- 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——Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- 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 ...
随机推荐
- thinkphp5使用空模块
今天想做一个功能,可以后台设置url是二级域名(也是指向同一个服务器)还是一级域名(域名/模块),网上找了找,TP3.2开始取消了空模块.所以只能自己修改框架源码了. ----------有点晚,明天 ...
- Charles抓包软件简介
Charles简介: Charles是一款抓包神器,因为他是基于 java 开发的,所以跨平台,Mac.Linux.Window下都是可以使用的,确保安装之前已经安装了JDK.Charles官网地址: ...
- Codeforces Round #555 (Div. 3) AB
A: http://codeforces.com/contest/1157/problem/A 题意:每次加到10的整数倍之后,去掉后面的0,问最多有多少种可能. #include <io ...
- 又到毕业季,尚学堂喊你免费领取100个Java毕设项目(含源码视频),限时一周哦!
你还在为毕设发愁?不知道该如何命题?不知道从哪里下手?担心毕设过不了影响毕业? 尚学堂首家隆重推出了刷爆朋友圈的毕设100个项目,别说你还没去下载观看!!最最重要的是:免费!免费!免费!而且限时一周! ...
- 一篇年薪60万的JVM性能调优文章
JVM 调优概述 性能定义 吞吐量 - 指不考虑 GC 引起的停顿时间或内存消耗,垃圾收集器能支撑应用达到的最高性能指标. 延迟 - 其度量标准是缩短由于垃圾啊收集引起的停顿时间或者完全消除因垃圾收集 ...
- [Swift]LeetCode275. H指数 II | H-Index II
Given an array of citations sorted in ascending order (each citation is a non-negative integer) of a ...
- [Swift]LeetCode357. 计算各个位数不同的数字个数 | Count Numbers with Unique Digits
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [Swift]LeetCode941. 有效的山脉数组 | Valid Mountain Array
Given an array A of integers, return true if and only if it is a valid mountain array. Recall that A ...
- Kubernetes---Pod的扩容和缩容
用RC的Scale机制来实现Pod的扩容和缩容 把redis-slave的pod扩展到3个 , kubectl scale rc redis-slave --replicas=3 现在来缩容,把 ...
- VIVO 手机重力传感器踩坑记录
手上的 vivo-x9 手机传感器模式下的旋转效果有误,经查发现是 Gravity sensor 返回的数据有误,和其他机型返回的数据相反的. 参考 Gravity 的说明: A three dime ...