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 ...
随机推荐
- NOIP 2015 信息传递
kawayi 题目描述 有n个同学(编号为1到n)正在玩一个信息传递的游戏.在游戏里每人都有一个固定的信息传递对象,其中,编号为i的同学的信息传递对象是编号为Ti同学. 游戏开始时,每人都只知道自己的 ...
- bzoj 3389
题意:给定1维连续T<= 1000000个点,以及n<=10000个线段,求最少的线段覆盖该区间.. 思路:很显然,贪心是可以做的..不过这一题最有意思的是使可以转换为最短路模型.. 如果 ...
- [MSSQL2012]LEAD函数
LEAD函数简单点说,就是把下一行的某列数据提取到当前行来显示,看示例更能解释清楚,先看测试用脚本 DECLARE @TestData TABLE( ID INT IDENTITY(1,1), ...
- 利用Junit4进行程序模块的测试,回归测试
①在你的工程里导入JUnit4的包 ②右击创建JUnit测试类,在测试类中编写测试代码即可. JUnit 目前需要掌握的有一下几点: Fixture系列:BeforeClass,AfterClass, ...
- Html5 学习系列(六)Html5本地存储和本地数据库
一个网站如何能在客户的浏览器存储更多的数据呢? 在Html4的时代在浏览器端存储点网站个性化的数据,尤其是用户浏览器的痕迹,用户的相关数据等一般只能存储在Cookie中,但是大多是浏览器对于Cooki ...
- 拟物设计和Angular的实现 - Material Design(持续更新)
Material Design是Google最新发布的跨平台统一视觉设计语言.直接翻译是物质设计,但是我更倾向于使用"拟物设计"更为准确. 据谷歌介绍,Material Desig ...
- DDD基本概念
一条箴言是:如果值对象是可共享的,那么它们应该是不可变的.值对象应该保持很小.很简单 极力推荐将值对象实现为不可变的.它们由一个构造器创建,并且在它们的生命周期内永远不会被修改.当你想要得到这个对象的 ...
- atitit.组件化事件化的编程模型--服务端控件(1)---------服务端控件与标签的关系
atitit.组件化事件化的编程模型--服务端控件(1)---------服务端控件与标签的关系 1. 服务器控件是可被服务器理解的标签.有三种类型的服务器控件: 1 1.1. HTML 服务器控件 ...
- Android Studio 生成Release版,报Warning的解决办法
转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6242227.html 请尊重知识产权!!! 同步更新到CSDN:http://blog.csdn.net/ ...
- Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...