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

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

思路: 二分法

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

class Solution {
public:
// find middle element of the list
ListNode *getmiddleList(ListNode *left,ListNode *right){
//omit the condition : left!=right && left->next!=right
ListNode *pre,*last;
pre=left; last =left->next;
while(last!=right){
last = last->next;
if(last!=right){
last = last->next;
pre=pre->next;
}
}
return pre;
} // retri-BST constructor
TreeNode *getBST(ListNode *left,ListNode *right){
TreeNode *root = new TreeNode();
//no leaf
if(left==right) return NULL;
// only one leaf
if(left->next == right){
root->val=left->val;
return root;
}
//more than one leaf
ListNode *middle =getmiddleList(left,right);
root->val = middle->val;
root->left = getBST(left, middle);
root->right = getBST(middle->next,right);
return root;
}
TreeNode *sortedListToBST(ListNode *head) {
TreeNode* root= new TreeNode();
if(head==NULL) return NULL;
if(head->next==NULL){
root->val=head->val;
root->left=root->right=NULL;
return root;
}
ListNode *left,*middle,*right;
middle=left=head;
right=head->next;
while(right){
right=right->next;
if(right){
right=right->next;
middle=middle->next;
}
}
root->val=middle->val;
root->left = getBST(left, middle);
root->right= getBST(middle->next,right);
return root;
}
};

转载请注明出处: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. 【Oracle】Oracle透明网关访问MSSQLServer

             Oracle 数据库的透明网关 ( transparent gateway )是这样的一个接口:通过它,我们可以 sqlplus 操纵其他数据库,如 MS SQLServer . s ...

  2. Halcon算子之shape_trans,用于变换区域的形状

    函数原型:shape_trans(Region : RegionTrans : Type : ) *shape_trans*仍然是区域,smallest_rectangle1可以获得四个角的坐标 函数 ...

  3. npm 全局环境变量配置

      我们要先配置npm的全局模块的存放路径以及cache的路径,例如我希望将以上两个文件夹放在NodeJS的主目录下,便在NodeJs下建立”node_global”及”node_cache”两个文件 ...

  4. jquery排序与动态添加option以及属性

    function getOrgansid() { url="<%=basePath%>/rest/bsc/organ/selectOrganSidAllList"; $ ...

  5. Proxmox 安装 dsm 黑群 备忘

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

  6. 【原】Coursera—Andrew Ng机器学习—编程作业 Programming Exercise 4—反向传播神经网络

    课程笔记 Coursera—Andrew Ng机器学习—课程笔记 Lecture 9_Neural Networks learning 作业说明 Exercise 4,Week 5,实现反向传播 ba ...

  7. springmvc jpa

    昨天帮同学搭建了一个springmvc+jpa+beetl模板引擎的项目环境,供参考. https://files.cnblogs.com/files/startnow/lntu-demo.zip 数 ...

  8. IT 360服务器监控

  9. jquery入门 修改网页背景颜色

    我们在浏览一些网站,尤其是一些小说网站的时候,都会有修改页面背景颜色的地方,这个功能使用jquery很容易实现. 效果图: show you code: <!doctype html> & ...

  10. linux系统文件的安全与特殊权限

    一. 文件与目录的原始属性 由于不希望文件具有可执行的原始权限,默认情况下,文件是没有可执行(x)权限的,因此文件的原始属性是:-rw-rw-rw(0666) 目录的原始属性全部开放,为:-rwxrw ...