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. RippleNet: Propagating User Preferences on the Knowledge Graph for Recommender Systems

    一.摘要 为了解决协同过滤的稀疏性和冷启动问题,社交网络或项目属性等辅助信息被用来提高推荐性能. 考虑到知识图谱是边信息的来源,为了解决现有的基于嵌入和基于路径的知识图谱感知重构方法的局限性,本文提出 ...

  2. SQL异常总结

    1.resultType和resultMap写错时,启动时就会报错 原因: 2.The error occurred while handling results ### SQL: select US ...

  3. python通过一句话判断闰年的代码

    [str(i)+'闰年' for i in range(2000,2100) if i%4 ==0 and i%100 !=0]

  4. GO语言开发之路

    Go语言开发之路 介绍 为什么学习Go语言? 开发环境准备 从零开始搭建Go语言开发环境 VS Code配置Go语言开发环境 基础 Go语言基础之变量和常量 Go语言基础之基本数据类型 Go语言基础之 ...

  5. nginx1.15.10配置使用非https访问返回403

    nginx版本号:nginx version: nginx/1.15.10 server { listen 443 default ssl; server_name app.test.com; if ...

  6. java代码操作word模板并生成PDF

    这个博客自己现在没时间写,等后面有时间了,自己再写. 这中需求是在实际的项目开发中是会经常遇到的. 下面我们先从简单入手一步一步开始. 1.首先,使用word创建一个6行两列的表格. 点击插入-6行2 ...

  7. HBase学习笔记之BulkLoad

    HBase学习之BulkLoad bulkload的学习以后再写文章. 参考资料: 1.https://blog.csdn.net/shixiaoguo90/article/details/78038 ...

  8. npm run dev 报错 iview TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

    运行npm run dev报这个错 然后找到 D:\text\vue\iview-admin\build\webpack.dev.config.js打开 将这一行代码: fs.write(fd, bu ...

  9. Codeforces Round #605 (Div. 3) D. Remove One Element(DP)

    链接: https://codeforces.com/contest/1272/problem/D 题意: You are given an array a consisting of n integ ...

  10. 26-ESP8266 SDK开发基础入门篇--编写WIFI模块 SmartConfig/Airkiss 一键配网

    https://www.cnblogs.com/yangfengwu/p/11427504.html SmartConfig/Airkiss 配网需要APP/微信公众号,这节大家先使用我做好的APP/ ...