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 ascending order, convert it to a height balanced BST.
SOLUTION 1:
public TreeNode sortedListToBST1(ListNode head) {
ListNode fast = head;
ListNode slow = head;
ListNode pre = head;
if (head == null) {
return null;
}
TreeNode root = null;
if (head.next == null) {
root = new TreeNode(head.val);
root.left = null;
root.right = null;
return root;
}
// get the middle node.
while (fast != null && fast.next != null) {
fast = fast.next.next;
// record the node before the SLOW.
pre = slow;
slow = slow.next;
}
// cut the list to two parts.
pre.next = null;
TreeNode left = sortedListToBST1(head);
TreeNode right = sortedListToBST1(slow.next);
root = new TreeNode(slow.val);
root.left = left;
root.right = right;
return root;
}
SOLUTION 2:
public TreeNode sortedListToBST(ListNode head) {
if (head == null) {
return null;
}
int size = ;
ListNode cur = head;
while (cur != null) {
size++;
cur = cur.next;
}
CurrNode curNode = new CurrNode(head);
return sortedListToBSTHelp(curNode, size);
}
public class CurrNode {
ListNode node;
CurrNode(ListNode node) {
this.node = node;
}
}
// when the recursion is done, the curr node should point to the node
// which is the next of the block.
public TreeNode sortedListToBSTHelp(CurrNode curr, int size) {
if (size <= ) {
return null;
}
TreeNode left = sortedListToBSTHelp(curr, size/);
// because we want to deal with the right block.
TreeNode root = new TreeNode(curr.node.val);
curr.node = curr.node.next;
TreeNode right = sortedListToBSTHelp(curr, size - - size/);
root.left = left;
root.right = right;
return root;
}
SOLUTION 3:
并且这个dfs有一个作用 会把指针移动到这个要建的树的下一个位置
经过这一行 cur就移动到了中间
我们建立 一个根 TreeNode root = new TreeNode(curNode.val);
把cur移动到下一个 curNode = curNode.next;
再用递归建立右树
root.right = right;
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; next = null; }
* }
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
ListNode curNode = null; public TreeNode sortedListToBST(ListNode head) {
if (head == null) {
return null;
} int size = ;
ListNode cur = head;
while (cur != null) {
size++;
cur = cur.next;
} curNode = head;
return dfs(head, size);
} // Use the size to control.
public TreeNode dfs(ListNode head, int size) {
if (size <= ) {
return null;
} TreeNode left = dfs(head, size / );
TreeNode root = new TreeNode(curNode.val); // move the current node to the next place.
curNode = curNode.next;
TreeNode right = dfs(curNode, size - size / - ); root.left = left;
root.right = right; return root;
}
}
LeetCode: Convert Sorted List to Binary Search Tree 解题报告的更多相关文章
- 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】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【原创】leetCodeOj ---Convert Sorted List to Binary Search Tree 解题报告
原题地址: https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题目内容: Given a sing ...
- 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 ...
随机推荐
- MyBatis框架——mybatis插入数据返回主键(mysql、oracle)
向数据库中插入数据时,大多数情况都会使用自增列或者UUID做为主键.主键的值都是插入之前无法知道的,但很多情况下我们在插入数据后需要使用刚刚插入数据的主键,比如向两张关联表A.B中插入数据(A的主键是 ...
- [原] XAF 如何非常容易禁止清除一个下拉字段的值?
- requireJS 用法
requireJS使用教程 2.0 常用方法 requirejs.config : 为模块指定别名 requirejs : 将写好的模块进行引入,根据模块编写主代码 define : 编写模块 htm ...
- linux内核调优详解
cat > /etc/sysctl.conf << EOF net.ipv4.ip_forward = net.ipv4.conf.all.rp_filter = net.ipv4. ...
- Makeflow 4.0 发布,工作流引擎
Makeflow 4.0 发布了,主要改进包括: 1. 支持分层次的 workers,带 master-foremen-workers 范式. 2. 一个 worker 可同时处理超过 1 个的任务3 ...
- 中文版Windows Server 2012 R2更改为英文显示语言
1. 下载Windows Server 2012 R2多语言包(下载页面,直接下载链接). 2. 将win_svr_2012_r2_64bit_multi_language_lp_oem.img解压或 ...
- Twitter API升级至1.1
Twitter API 1.1是至今最大的一次升级,从3月份提出,到6月11日1.0版本已经全面停止调用.关于1.1版本升级特性可访问: https://dev.twitter.com/docs/ap ...
- JQuery ajax url传值与data传值的区别
url传中文,乱码,即便charset为 UTF-8, $.ajax({ type: "POST", cache: false, url: "/Prod ...
- 看2016上半年O2O新风向,太阳终会穿破乌云
纵观我国的O2O行业发展历程,去年上半年还处于资本的投资热潮,下半年就遭遇到了寒冬的突袭,使得很多才刚刚发芽的O2O企业直接被一阵寒风给吹倒.但同样的,一阵风浪过后才知道在O2O这片战场上谁才是有实力 ...
- elclipse/myeclipse web.xml自动提示补全问题
默认情况下,在编辑web.xml时是没有自动提示功能的,只能在编辑完成保存时验证语法是否正确. 解决方法: 1.下载(保存)http://java.sun.com/xml/ns/j2ee/web-ap ...