Given a singly linked list where elements are sorted in ascending order,

convert it to a height balanced BST.

==============

题目:将升序的链表,转化为BST(note:此BST要求是高度平衡,就是树种左右子树高度差不超过1)

思路:

def TreeNode* sortedListToBST(ListNode* head){

    按照slow/fast方式找到链表的中间节点mid,

    将升序链表在mid节点处切割成为两个链表head1,和head2

    将中间节点的值,new一个新的TreeNode节点:TreeNode *root = new TreeNode(head2->val)

    下面开始递归:
root的左子树就是sortedListToBST(head1)
root的右子树就是sortedListToBST(head2->next)
return root
}

代码如下:

/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
if(head==nullptr){
return nullptr;
}else if(head->next==nullptr){
return new TreeNode(head->val);
} ListNode *slow,*fast,*prev;
slow = fast = head;
prev = nullptr;
while(fast && fast->next){
prev = slow;
slow = slow->next;
fast = fast->next->next;
}
prev->next = nullptr;
//showList(head);
//showList(slow);
TreeNode *root = new TreeNode(slow->val);
root->left = sortedListToBST(head);
root->right = sortedListToBST(slow->next);
return root;
}
};

109. Convert Sorted List to Binary Search Tree的更多相关文章

  1. 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 ...

  2. leetcode 108. Convert Sorted Array to Binary Search Tree 、109. Convert Sorted List to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree 这个题使用二分查找,主要要注意边界条件. 如果left > right,就返回NULL.每次更新的 ...

  3. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

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

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

  5. 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. [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 ...

  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. 【Leetcode】109. Convert Sorted List to Binary Search Tree

    Question: Given a singly linked list where elements are sorted in ascending order, convert it to a h ...

  9. 109. Convert Sorted List to Binary Search Tree (List; Divide-and-Conquer, dfs)

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

随机推荐

  1. Codeforces Round #366 (Div. 2) A

    A. Hulk time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  2. iPhone4@iOS7Beta4,第一时间刷上,失望,看来苹果是铁了心往扁平化UI走了。看好我的614,保存好SHSH准备

    1 今天早上看到新闻,iOS7Beta4放出了,于是赶紧,在家下载,网速很快.(要是在公司,那50K的速度,估计会疯的) 2 等了一会儿一直在提示准备安装,不等了,再等该迟到了. 3 路上实在忍不住, ...

  3. phpwind8.7升级9.0.1过程(三)20130107升级到20130227

    升级之前一定要备份以下内容 1.网站根目录所有文件 2.网站数据库 在升级到20130227版本的时候出现以下问题. 问题: 问题原因:本地服务器不支持fsockopen()函数,此函数需要开启,但是 ...

  4. Java—面向对象—权限修饰符及思维导图

    课上老师所讲实例整理: package org.hanqi.pn0120; //汽车 public class Car { //颜色 private String yanse; //品牌 privat ...

  5. Ubuntu安装Mysql+Django+MySQLdb

    安装Mysql sudo apt-get install mysql-server mysql-client root@IdeaPad:/# mysql -u root -p Enter passwo ...

  6. iOS 9.0中UIAlertController的用法

    UIAlertView和UIActionSheet 被划线了. 苹果不推荐我们使用这两个类了.也不再进行维护和更新 正如苹果所说它现在让我们用UIAlertConntroller 并设置样式为UIAl ...

  7. sessionStorage和localStorage中 存储

    转:http://my.oschina.net/crazymus/blog/371757 sessionStorage只在页面打开是起作用, localStorage关闭页面后仍然起作用. 有时候,我 ...

  8. Unity Shader _Time

    _Time是个4维向量,跟Unity3D中的deltaTime(这是个一维的,数值)不同. float4 _Time : Time (t/20, t, t*2, t*3), use to animat ...

  9. 在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke 解决办法

    增加IsHandleCreated 判断 if (this.IsHandleCreated) { this.Invoke(new EventHandler(delegate { ...... })); ...

  10. Unity垃圾回收

    值类型:内置类型及结构体. 引用类型:对象.字符串.数组. GC的噩梦:连续字符串相加           System.Text.StringBuilder function ConcatExamp ...