Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

Given the sorted linked list: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
/ \
-3 9
/ /
-10 5

108. Convert Sorted Array to Binary Search Tree 思路一样,只不过一个是数组,一个是链表。数组可以通过index直接访问元素找到中点,而链表的查找中间点要通过快慢指针来操作。

Java:

class Solution {
public TreeNode sortedListToBST(ListNode head) {
if(head==null) return null;
return toBST(head,null);
}
public TreeNode toBST(ListNode head, ListNode tail){
ListNode slow = head;
ListNode fast = head;
if(head==tail) return null; while(fast!=tail&&fast.next!=tail){
fast = fast.next.next;
slow = slow.next;
}
TreeNode thead = new TreeNode(slow.val);
thead.left = toBST(head,slow);
thead.right = toBST(slow.next,tail);
return thead;
}
}   

Python:

class ListNode:
def __init__(self, x):
self.val = x
self.next = None class Solution:
head = None
# @param head, a list node
# @return a tree node
def sortedListToBST(self, head):
current, length = head, 0
while current is not None:
current, length = current.next, length + 1
self.head = head
return self.sortedListToBSTRecu(0, length) def sortedListToBSTRecu(self, start, end):
if start == end:
return None
mid = start + (end - start) / 2
left = self.sortedListToBSTRecu(start, mid)
current = TreeNode(self.head.val)
current.left = left
self.head = self.head.next
current.right = self.sortedListToBSTRecu(mid + 1, end)
return current

Python:

def sortedListToBST(self, head):
if not head:
return
if not head.next:
return TreeNode(head.val) slow, fast = head, head.next.next
while fast and fast.next:
fast = fast.next.next
slow = slow.next tmp = slow.next
slow.next = None
root = TreeNode(tmp.val)
root.left = self.sortedListToBST(head)
root.right = self.sortedListToBST(tmp.next) return root  

C++:

class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
auto curr = head;
int n = 0;
while (curr) {
curr = curr->next;
++n;
}
return BuildBSTFromSortedDoublyListHelper(&head, 0, n);
} TreeNode * BuildBSTFromSortedDoublyListHelper(ListNode **head, int s, int e) {
if (s == e) {
return nullptr;
} int m = s + ((e - s) / 2);
auto left = BuildBSTFromSortedDoublyListHelper(head, s, m);
auto curr = new TreeNode((*head)->val); *head = (*head)->next;
curr->left = left;
curr->right = BuildBSTFromSortedDoublyListHelper(head, m + 1, e);
return curr;
}
};

  

  

类似题目:

[LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树

All LeetCode Questions List 题目汇总

[LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树的更多相关文章

  1. [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  2. LeetCode 108. Convert Sorted Array to Binary Search Tree (有序数组转化为二叉搜索树)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目 ...

  3. 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. 108. Convert Sorted Array to Binary Search Tree 109. Convert Sorted List to Binary Search Tree -- 将有序数组或有序链表转成平衡二叉排序树

    108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...

  5. Leetcode#109 Convert Sorted List to Binary Search Tree

    原题地址 跟Convert Sorted Array to Binary Search Tree(参见这篇文章)类似,只不过用list就不能随机访问了. 代码: TreeNode *buildBST( ...

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

  7. leetcode 109 Convert Sorted List to Binary Search Tree ----- java

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  8. Java for LeetCode 109 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]109. Convert Sorted List to Binary Search Tree链表构建二叉搜索树

    二叉树的各种遍历方式都是可以建立二叉树的,例如中序遍历,就是在第一步建立左子树,中间第二步建立新的节点,第三步构建右子树 此题利用二叉搜索树的中序遍历是递增序列的特点,而链表正好就是递增序列,从左子树 ...

随机推荐

  1. Codeforces F. Vus the Cossack and Numbers(贪心)

    题目描述: D. Vus the Cossack and Numbers Vus the Cossack has nn real numbers aiai. It is known that the ...

  2. NOIP2017 PJ 跳房子 —— 单调队列优化DP

    题目描述 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一.跳房子的游戏规则如下: 在地面上确定一个起点,然后在起点右侧画n个格子,这些格子都在同一条直线上.每个格子内有一个 ...

  3. 从url中下载资源(目前测试只有照片,文件类的没有进行测试)

    首先:是工具类: public class DownLoadUtils { /** * 从网络Url中下载文件 * * @param urlStr url路径 * @param fileName 文件 ...

  4. Alpha冲刺(10/10)——2019.5.3

    所属课程 软件工程1916|W(福州大学) 作业要求 Alpha冲刺(10/10)--2019.5.3 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪 ...

  5. 编程小白入门分享三:Spring AOP统一异常处理

    Spring AOP统一异常处理 简介 在Controller层,Service层,可能会有很多的try catch代码块.这将会严重影响代码的可读性."美观性".怎样才可以把更多 ...

  6. This content should also be served over HTTPS

    HTTPS 是 HTTP over Secure Socket Layer,以安全为目标的 HTTP 通道,所以在 HTTPS 承载的页面上不允许出现 http 请求,一旦出现就是提示或报错: jqu ...

  7. VS2010中使用boost正则表达式库

    1.下载boost库.http://www.boost.org/ 我下载的是boost_1_51_0版本.放在D:\opensource\boost_1_51_0. 2.编译boost库.     执 ...

  8. BZOJ 4890: [Tjoi2017]城市 树形dp

    标签:树形dp,枚举,树的直径 一上来看到这个题就慌了,只想到了 $O(n^3)$ 的做法. 碰到这种题时要一步一步冷静地去分析,观察数据范围. 首先,$n\leqslant 5000$,所以可以先 ...

  9. javascript 终极循环方法for... of ..推荐

    js目前有很多的循环方法,如for, forEach,  for .. in,  for of 等等,而在ES6里面,我们又增加了一些数据结构,比如set,map,Symbol等. 那么我们该选取哪一 ...

  10. 如何抓取微信小程序的源码?

    一.引言: 在工作中我们会想把别人的代码直接拿过来进行参考,当然这个更多的是前端代码的进行获取. 那么微信小程序的代码怎么样获取呢?  参考 https://blog.csdn.net/qq_4113 ...