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. dede模版列表调用文章正文内容的方法

    在制作织梦模板的时候,有的时候我们需要调用文章部分内容,用[field:description/]标签字数不够多(数据库设计字段是varchar(255)的),另外修改了文章内容但是摘要还需要手动修改 ...

  2. resin的基本操作

    1.什么是resin?     resin是CAUCHO公司的产品,是一个非常流行的支持servlets和jsp的引擎,速度非常快.Resin本身包含了一个支持HTTP/1.1的WEB服务器.虽然它可 ...

  3. NOI 2004 郁闷的出纳员(平衡树)

    题目描述 OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作,但是令人郁闷的是,我们的老板反复无常,经常调整员工的工资 ...

  4. ARM体系的7种工作模式

    一.ARM体系的CPU有以下7种工作模式:   用户模式(usr)    大多数程序运行于用户模式 特权模式   系统模式(sys)   运行具有特权的操作系统任务 异常模式 中断模式(irq)   ...

  5. python模块结构和布局

    用模块来合理的组织你的python代码是简单又自然的方法.下面介绍一种非常合理的布局: #(1)起始行(Unix) #(2)模块文档 #(3)模块导入 #(4)变量定义 #(5)类定义 #(6)函数定 ...

  6. php 错误 Strict Standards: PHP Strict Standards: Declaration of .... should be compatible with that of 解决办法

    错误原因:这是由于 php 5.3版本后.要求继承类必须在父类之后定义.否则就会出现Strict Standards: PHP Strict Standards: Declaration of ... ...

  7. python_day7【模块configparser、XML、requests、shutil、系统命令-面向对象】之篇

    python内置模块补充 一.configparser configparser:用户处理特定格式的文件,其本质是利用open打开文件 # 节点 [section1] #键值对k1 = v1 k2:v ...

  8. Oracle数据库——半期测验

    一.使用system用户登录SQL*PLUS,使用命令将scott用户解锁,并将scott用户的密码修改为: t_你的学号后三位(例如:t_165).然后,以scott用户连接数据库. 1. 使用sy ...

  9. maxsdk sample中3dsexp.rc点不开并提示specstrings.h中找不到sal.h解法

    在网上下载sal.h文件并拷贝到specstrings.h所在目录(C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Include)即可. sa ...

  10. 转载:scikit-learn学习之SVM算法

    转载,http://blog.csdn.net/gamer_gyt 目录(?)[+] ========================================================= ...