给定有序链表(元素由小到大), 试问如何将其转换为一个平衡BST?

平衡BST: 任意节点的左右子树的深度差值不大于1.

主要思想是用递归. Trick是使用快慢指针来获取中间节点. 获得中间节点后, 将其设为此次递归的root, 随后删除此节点, 并将前一节点的next置NULL. 随后, 对中间节点的前后部分分别进行递归调用, 并将返回值作为其左右子树.

代码如下:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for binary tree
* 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 == NULL) return NULL;
if(head->next == NULL) return new TreeNode(head->val);
ListNode *step1 = head;
ListNode *step2 = head->next;
while(step2->next != NULL && step2->next->next != NULL){
step1 = step1->next;
step2 = step2->next->next;
}
TreeNode *root = new TreeNode(step1->next->val);
ListNode *head2 = step1->next->next;
delete step1->next;
step1->next = NULL; // cut list into two parts
root->left = sortedListToBST(head);
root->right = sortedListToBST(head2);
return root;
}
};

[LeetCode系列]有序链表转换为平衡BST的递归解法的更多相关文章

  1. [LeetCode] 109. 有序链表转换二叉搜索树

    题目链接 : https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/ 题目描述: 给定一个单链表,其中的 ...

  2. LeetCode 中级 - 有序链表转换二叉搜索树(109)

    给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10 ...

  3. LeetCode 109. 有序链表转换二叉搜索树(Convert Sorted List to Binary Search Tree)

    题目描述 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: ...

  4. Java实现 LeetCode 109 有序链表转换二叉搜索树

    109. 有序链表转换二叉搜索树 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. ...

  5. 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST

    引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...

  6. Convert Sorted List to Binary Search Tree——将链表转换为平衡二叉搜索树 &&convert-sorted-array-to-binary-search-tree——将数列转换为bst

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

  7. LeetCode 109——有序链表转化二叉搜索树

    1. 题目 2. 解答 2.1. 方法一 在 LeetCode 108--将有序数组转化为二叉搜索树 中,我们已经实现了将有序数组转化为二叉搜索树.因此,这里,我们可以先遍历一遍链表,将节点的数据存入 ...

  8. [LeetCode系列]翻转链表问题II

    给定一个链表和两个整数m, n, 翻转链表第m个节点到第n个节点(从1开始计数). 如, 给定链表: 1->2->3->4->5->NULL, 以及 m = 2, n = ...

  9. [LeetCode] 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 ...

随机推荐

  1. Android----Material Design之(FloatActionButton,CoordinatorLayout,CollapsingToolbarLayout,AppBarLayout,TabLayout等)

    Material Design 的一些UI 平常开发还是用的比较多的,以前没写,最近总结一下,写一篇博客,要求版本在5.0以上. 主要介绍了FloatActionButton,CoordinatorL ...

  2. kotlin for android----------MVP模式下(OKHttp和 Retrofit+RxJava)网络请求的两种实现方式

    今天要说的干货是:以Kotlin,在MVP模式下(OKHttp和 Retrofit+RxJava)网络请求两种实现方式的一个小案例,希望对大家有所帮助,效果图: Retrofit是Square公司开发 ...

  3. 1-15-2-RAID1 企业级RAID磁盘阵列的搭建(RAID1、RAID5、RAID10)

    大纲: 1.创建RAID1 2.创建RAID5 3.创建RAID10 =============================== 1.创建RAID1 RAID1原理:需要两块或以上磁盘,可添加热备 ...

  4. Day34 设计模式

    参考博客: http://www.cnblogs.com/alex3714/articles/5760582.html 什么是设计模式 Christopher Alexander:“每一个模式描述了一 ...

  5. BGP&RIP

    策略路由是2000之前 如果不通检查是否有相互影响的策略 BGP 25端 查看 25是上端起lan 地址是172.100.1.254 互联158是下端起wan 172.100.1.158   25的内 ...

  6. Spring入门4.AOP配置深入

    Spring入门4.AOP配置深入 代码下载 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 之前学习AOP中的一些概念,包括连接点.切入点(pointc ...

  7. c# out ref 多个返回值问题个人总结

    多个返回值可以用ref或者out来实现 如 var b=string.Empty(); var c=string.Empty(); public bool Test(string a, out str ...

  8. Http权威指南(服务器、缓存)

    对于web服务器(软件)大家应该不会陌生,常见的web服务器有Apache.IIS.Tomcat.Nginx.Jetty等等. 1.基本功能 几乎所有的web服务器都会执行以下几项同样的任务: 1.建 ...

  9. EasyPlayer RTSP播放器:一个适用于安防行业的工具利器(EasyPlayer Windows v2.0.17.0709)

    本文转自EasyDarwin开源团队成员Sword的博客:http://blog.csdn.net/swordtwelve EasyPlayer(Windows) v2.0.17.0709版本又更新发 ...

  10. CSS样式让元素填充剩余部分为自己的高度或宽度

    #nav {     background-color: #85d989;     width: 100%;     height: 50px; } #content {     background ...