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. python学习——练习题(8)

    """ 题目:输出 9*9 乘法口诀表. """ def answer1(): """ 自己用最普通的双重循环 ...

  2. 【译】PGS字幕

    PGS(Presentation graphic stream):图形字幕流,是用来显示蓝光电影中的字幕的流.当蓝光盘中的PGS格式的字幕被分离存储的时候通常保存在一个以sup为扩展名的文件中.(也可 ...

  3. VS2013 查看程序各个函数的CPU利用率<转>

    自己写的程序CPU占用率过高,无法锁定原因时,可以用VS2013帮忙检测 1. 打开VS 性能分析 2. 启动项目进行检测 3. 选择CPU采样 完成 4. 分析一段时间 然后停止分析 5. 选择显示 ...

  4. effective javascript 学习心得

    第2条:理解JavaScript的浮点数 1.js数字只有一种类型 2.见代码 /** * Created by Administrator on 2017/7/2. */ console.log(& ...

  5. LUA使用虚函数与使用回调函数

    ------------------虚函数overload-------------------------- --回调:寻路中格子坐标改变 CHero.OnSearchToCellsChange = ...

  6. 【300】◀▶ IDL - ENVI API

    参考:ENVI API 参考:ENVI Classic Display 序号 类名称   功能说明   语法 & 举例 01 ENVI 函数   ====<<<< De ...

  7. 常用sql语句备份

    1.查看数据库中所有的存储过程 use [DataBase_Name] go SELECT * FROM sys.all_objects WHERE ([type] = 'P' OR [type] = ...

  8. 迷你MVVM框架 avalonjs 组件编写指南

    avalon经过半年的宣传,已经有不少公司在使用avalon应用于它们内外网应用或移动项目,比较大牌的客户有百度,搜狐,金山,边缘,去哪儿--最近成为去哪儿的前端架构师后,掌握更多资源,可以随使抓个人 ...

  9. Ubuntu 安装 kamailio

    首先安装前,你已经对kamailio的基本用法了解.可根据情况选择安装方式,本次安装基于Ubuntu18.04系统安装,对于16.04及一下会遇到版本问题,请自己查阅文档解决 安装第三方库 sudo ...

  10. react native android 真机调试

    http://localhost:8081/index.android.bundle?platform=android 晕死,设备掉线了 C:\Users\ZHONGZHENHUA\.android\ ...