Convert Sorted List to Binary Search Tree leetcode java
题目:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
题解:
之前做过一道是从sorted array转换到BinarySearchTree的,方法还是一样二分法。但是构造树的方法不是由顶至下了,是要由低至上的建立。
代码如下:
1 static ListNode h;
2
3 public TreeNode sortedListToBST(ListNode head) {
4 if (head == null)
5 return null;
6
7 h = head;
8
9 int len = 0;
ListNode temp = head;
while(temp != null){
len++;
temp = temp.next;
}
return sortedListToBST(0, len - 1);
}
public TreeNode sortedListToBST(int start, int end) {
if (start > end)
return null;
int mid = (start + end) / 2;
TreeNode left = sortedListToBST(start, mid - 1);
TreeNode root = new TreeNode(h.val);
root.left = left;
h = h.next;
TreeNode right = sortedListToBST(mid + 1, end);
root.right = right;
return root;
}
Reference: http://www.programcreek.com/2013/01/leetcode-convert-sorted-list-to-binary-search-tree-java/
Convert Sorted List to Binary Search Tree leetcode java的更多相关文章
- Convert Sorted Array to Binary Search Tree leetcode java
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- Convert Sorted List to Binary Search Tree [LeetCode]
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- Convert Sorted Array to Binary Search Tree || LeetCode
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...
- Convert Sorted Array to Binary Search Tree——LeetCode
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目 ...
- LeetCode算法题-Convert Sorted Array to Binary Search Tree(Java实现)
这是悦乐书的第166次更新,第168篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第25题(顺位题号是108).给定一个数组,其中元素按升序排序,将其转换为高度平衡的二叉 ...
- 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 OJ】Convert Sorted Array to Binary Search Tree
Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...
随机推荐
- BZOJ.2705.[SDOI2012]Longge的问题(莫比乌斯反演 欧拉函数)
题目链接 \(Description\) 求\[\sum_{i=1}^n\gcd(i,n)\] \(Solution\) \[ \begin{aligned} \sum_{i=1}^n\gcd(i,n ...
- Codeforces Round #272 (Div. 2) E. Dreamoon and Strings 动态规划
E. Dreamoon and Strings 题目连接: http://www.codeforces.com/contest/476/problem/E Description Dreamoon h ...
- android studio 使用总结
网站1:http://stormzhang.com/posts.html 网站2:http://blog.csdn.net/hyr83960944/article/details/38388429
- ip定位
http://www.cnblogs.com/pengcc/p/5294836.html https://wx.jdcloud.com/shop/shopDetail/RTBAsia,里面有各种IP地 ...
- Hadoop系列之(二):Hadoop集群部署
1. Hadoop集群介绍 Hadoop集群部署,就是以Cluster mode方式进行部署. Hadoop的节点构成如下: HDFS daemon: NameNode, SecondaryName ...
- Sublime Text 2 快捷键(转)
文件 File 新建文件 Ctrl + N 打开文件 Ctrl + O 打开最近关闭的文件 Ctrl + Shift + T 保存 Ctrl + S 另存为… Ctrl + Shift + S 关闭文 ...
- Java 线程第三版 第四章 Thread Notification 读书笔记
一.等待与通知 public final void wait() throws InterruptedException 等待条件的发生. public final void wait(lo ...
- LPC18xx and LPC43xx 选型及差异
Q LPC43xx和LPC18xx有相同的USB接口以及片内USB驱动吗?A 是的. Q LPC4300和LPC1800只是内核不同吗?外设和管脚配置都完全一致?A 为LPC18xx的映像可以直接在L ...
- WCID Devices -- Windows Compatible ID Devices
WCID Devices What is WCID? A WCID device, where WCID stands for "Windows Compatible ID", i ...
- cmd 递归删除目录或文件
递归删目录 for /r <TARGET DIR> %i in (<DIR NAME or Pattern>) do rd /s /q %i 递归删文件 for /r < ...