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

  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 list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

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

  4. [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. 这道 ...

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

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

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

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

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

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

随机推荐

  1. Python PE8 编程规范

    1.使用四个空格而不是tab进行缩进 2.默认使用utf-8编码 3.尽量不要使用魔术方法 4.类中使用self作为默认参数 5.命名时,尽量使用驼峰式或单词+下划线,要保证见名知意 6.操作符和逗号 ...

  2. vue解决加载闪烁问题

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>& ...

  3. ES6新增的常用数组方法(forEach,map,filter,every,some)

    ES6新增的常用数组方法 let arr = [1, 2, 3, 2, 1]; 一 forEach => 遍历数组 arr.forEach((v, i) => { console.log( ...

  4. JS DOM事件学习

    DOM查找方法: document.getElementByID("id") document.getElementsByTagName("tag") 返回一个 ...

  5. Linux一键安装宝塔控制面板

    Linux一键安装宝塔的命令行 yum install -y wget && wget -O install.sh http://download.bt.cn/install/inst ...

  6. shuffle的工作原理

    Shuffle的正常意思是洗牌或弄乱,可能大家更熟悉的是Java API里的Collections.shuffle(List)方法,它会随机地打乱参数list里的元素顺序.如果你不知道MapReduc ...

  7. PHP 反射的简单使用

    反射机制简介 之前已经介绍过Java反射机制的简单使用,所有的反射机制的思想作用等都是类似的,下面就一起来了解一下PHP反射机制. 个人理解:反射机制就是可以利用类名或者一个类的对象来获取关于这个类的 ...

  8. java byte 梳理

    最近写编解码的代码比较多,抽一点时间梳理下java下byte的解析.在例子代码中主要涉及的知识点就两块: 1.byte代表8个bit,其中最高位是符号位: 2.当我们用String类的getBytes ...

  9. 企业IT管理员IE11升级指南【14】—— IE11代理服务器配置

    企业IT管理员IE11升级指南 系列: [1]—— Internet Explorer 11增强保护模式 (EPM) 介绍 [2]—— Internet Explorer 11 对Adobe Flas ...

  10. jsplumb 中文教程

    https://wdd.js.org/jsplumb-chinese-tutorial/#/ 1. jsplumb 中文基础教程 后续更新会在仓库:https://github.com/wangdua ...