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 题目汇总

最新文章

  1. R语言实战(一)介绍、数据集与图形初阶
  2. bzoj1012
  3. SqlServer中计算列详解
  4. Mac下显示隐藏文件 以及修改 hosts文件内容
  5. 获取XMLHttpRequest对象
  6. ViewPagerIndicator的使用方法
  7. Windows下Python读取GRIB数据
  8. 可视化Git版本管理工具SourceTree的使用
  9. 201521123109《java程序设计》第六周学习总结
  10. js回到顶部------转载
  11. idea操作整理
  12. centos7下kubernetes(6。运行应用)
  13. 通过jquery 获取用户当前所在的城市名称和IP地址
  14. 【Python】【辅助程序】练手小程序:记录外网动态IP地址
  15. C++中的智能指针
  16. 1分钟试用PowerShell 5.0新功能PowerShellGet安装Script Browser和Script Analyzer
  17. CH 0101 - a^b / CH 0102 - 64位整数乘法 - [快速幂和快速乘]
  18. hihoCoder week7 完全背包
  19. postgresql-日志表
  20. Execute Javascript in iOS Applications

热门文章

  1. 玩转DNS服务器——Bind服务
  2. 解决SELinux导致Apache更改端口后无法启动的问题
  3. MyBatis框架之注解开发
  4. scala 中的匹配模式
  5. kvm创建windows2008虚拟机
  6. 爬虫 - 解析库之Beautiful Soup
  7. Go语言在国产CPU平台上应用前景的探索与思考
  8. Windows环境下设置Tomcat8以服务的形式运行,不再打开Tomcat窗口
  9. Bell数入门
  10. 铺砖头问题(完美)——爆搜&&插头DP