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 ...
随机推荐
- user-agent | what is the "user-agent" ?
User Agent(用户代理) UA是一个特殊字符串头,使得服务器能够识别客户使用的操作系统及版本.CPU 类型.浏览器及版本.浏览器渲染引擎.浏览器语言.浏览器插件等 通过抓包可以得到 下面是几个 ...
- sqlzoo:5
展示世界的總人口. SELECT sum(population) FROM world 列出所有的洲份, 每個只有一次. select distinct(continent) from world 找 ...
- BZOJ5316 : [Jsoi2018]绝地反击
若$R=0$,那么显然答案为离原点最远的点到原点的距离. 否则若所有点都在原点,那么显然答案为$R$. 否则考虑二分答案$mid$,检查$mid$是否可行. 那么每个点根据对应圆交,可以覆盖圆上的一部 ...
- jquery ajax中success与complete的执行顺序
jquery ajax中success与complete的执行顺序 jquery中各个事件执行顺序如下: 1.ajaxStart(全局事件) 2.beforeSend 3.ajaxSend(全局事件) ...
- 关于VB里判断逻辑的说明
如上图,当进行连续判断的时候,即使第一个已经不符合条件了,后面的依然会计算.这点一定要记住,除非你所有的函数都有必要执行,否则会导致效率降低. 减代码不一定能提高效率,对于IIF和连续判断写法,貌似很 ...
- Linux下Shell重定向
1. 标准输入,标准输出与标准错误输出 Linux下系统打开3个文件,标准输入,标准输出,标准错误输出. 标准输入:从键盘输入数据,即从键盘读入数据. 标准输出:把数据输出到终端上. 标准错误输出:把 ...
- centos7安装kubeadm
安装配置docker v1.9.0版本推荐使用docker v1.12, v1.11, v1.13, 17.03也可以使用,再高版本的docker可能无法正常使用. 测试发现17.09无法正常使用,不 ...
- 快速幂 ,快速幂优化,矩形快速幂(java)
快速幂形式 public static int f(int a,int b,int c){ int ans =1; int base=a; while(b!=0){ if((b&1)!=0) ...
- (一) sublime安装和使用
1 下载安装sublime 可以破解也可以不破解 2 html基础架子自动生成插件Emmet的安装 3 Emmet 安装失败解决 4 快捷键设置和汇总 4 其他sublime插件汇总
- C语言复习5_调试
使用CodeBlocks调试程序 首先要注意,只有打开projects(.cbp文件)的情况下才能debug,单独打开.c文件是不能debug的 1.在行号旁边左键,出现红点,表示为断点breakpo ...