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

题意:给定一个有序的链表,将其转换成平衡二叉搜索树

思路: 二分法

要构建一个平衡二叉树,二分法无疑是合适的,至于如何分是的代码简洁,就需要用到递归了。

  1. class Solution {
  2. public:
  3. // find middle element of the list
  4. ListNode *getmiddleList(ListNode *left,ListNode *right){
  5. //omit the condition : left!=right && left->next!=right
  6. ListNode *pre,*last;
  7. pre=left; last =left->next;
  8. while(last!=right){
  9. last = last->next;
  10. if(last!=right){
  11. last = last->next;
  12. pre=pre->next;
  13. }
  14. }
  15. return pre;
  16. }
  17.  
  18. // retri-BST constructor
  19. TreeNode *getBST(ListNode *left,ListNode *right){
  20. TreeNode *root = new TreeNode();
  21. //no leaf
  22. if(left==right) return NULL;
  23. // only one leaf
  24. if(left->next == right){
  25. root->val=left->val;
  26. return root;
  27. }
  28. //more than one leaf
  29. ListNode *middle =getmiddleList(left,right);
  30. root->val = middle->val;
  31. root->left = getBST(left, middle);
  32. root->right = getBST(middle->next,right);
  33. return root;
  34. }
  35. TreeNode *sortedListToBST(ListNode *head) {
  36. TreeNode* root= new TreeNode();
  37. if(head==NULL) return NULL;
  38. if(head->next==NULL){
  39. root->val=head->val;
  40. root->left=root->right=NULL;
  41. return root;
  42. }
  43. ListNode *left,*middle,*right;
  44. middle=left=head;
  45. right=head->next;
  46. while(right){
  47. right=right->next;
  48. if(right){
  49. right=right->next;
  50. middle=middle->next;
  51. }
  52. }
  53. root->val=middle->val;
  54. root->left = getBST(left, middle);
  55. root->right= getBST(middle->next,right);
  56. return root;
  57. }
  58. };

转载请注明出处:http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: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] 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 ...

  3. [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree

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

  4. 【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 as ...

  5. 【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 ...

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

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

  7. leetcode -day19 Convert Sorted List to Binary Search Tree

    1.  Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted ...

  8. 37. leetcode 108. Convert Sorted Array to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...

  9. [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)

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

随机推荐

  1. Installation failed with message Failed to finalize session : INSTALL_FAILED_INVALID_APK: Split lib_slice_8_apk was defined multiple times. It is possible that this issue is resolved by uninstalling a

    取消:Instant Run就行

  2. 「小程序JAVA实战」Springboot版mybatis逆向生成工具(32)

    转自:https://idig8.com/2018/08/29/xiaochengxujavashizhanspringbootbanmybatisnixiangshengchenggongju32/ ...

  3. then()方法是异步执行

    then()方法是异步执行 就是当.then()前的方法执行完后再执行then()内部的程序 这样就避免了,数据没获取到等的问题

  4. JDBC中,如何动态的设置查询条件

    今天看JDBC,发现有段代码,可以减少重复的编写查询方法,如下: public List<Goddess> query(List<Map<String, Object>& ...

  5. Proxmox 安装 dsm 黑群 备忘

    备忘:Proxmox 虚拟机使用 E1000网卡(用Virlo找不到引导),直通数据硬盘 .     使用的引导文件是 DS918+_6.21-23824-1.04b.img 虚拟机启动使用第三项EX ...

  6. 将DataTable中的数据导出成Excel

    public bool ExportFile(System.Data.DataTable dt){    SaveFileDialog sfd = new SaveFileDialog();    s ...

  7. 一道面试题Lintcode196-Find the Missing Number

    http://www.lintcode.com/en/problem/find-the-missing-number/# Find the Missing Number Given an array ...

  8. 快速上手Runtime(二)之给分类添加属性

    我们都知道,分类是不能直接添加属性的,那么我们有时候又需要实现这个功能,那么我们应该怎么办才能为分类添加上属性呢. Runtime给分类添加属性原理 给一个类声明属性,其实本质就是给这个类添加关联,并 ...

  9. SQL 数据库 学习 003 什么是数据库? 为什么需要数据库?是不是所有的软件都是用Sql Server?

    什么是数据库? 为什么需要数据库? 是不是所有的软件都是用Sql Server? 我的电脑系统: Windows 10 64位 使用的SQL Server软件: SQL Server 2014 Exp ...

  10. 无需写try/catch,也能正常处理异常

    对于企业应用的开发者来说,异常处理是一件既简单又复杂的事情.说其简单,是因为相关的编程无外乎try/catch/finally+throw而已:说其复杂,是因为我们往往很难按照我们真正需要的策略来处理 ...