给定有序链表(元素由小到大), 试问如何将其转换为一个平衡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. HDU-2196-树形dp/计算树上固定起点的最长路

    Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. iptables详解(10):iptables自定义链

    前文中,我们一直在定义规则,准确的说,我们一直在iptables的默认链中定义规则,那么此处,我们就来了解一下自定义链. 你可能会问,iptables的默认链就已经能够满足我们了,为什么还需要自定义链 ...

  3. springboot拦截器注入service为空

    一般都是因为除了在拦截器之外,还需要在拦截器的配置类中,注册拦截器时没有使用spring的bean,而是使用了new创建bean造成的. @Configuration public class Web ...

  4. magento: Your web server is configured incorrectly. As a result, configuration files with sensitive information are accessible from the outside 解决方案

    在linux(以UBUNTU, CENTOS为例)下安装完成magento时,在进入后台时, 有些童鞋可能会发现有如下的提示: Your web server is configured incorr ...

  5. laravel5.5种的Eloquent ORM的使用:

    控制器方法: //Eloquent ORM的使用: public function orm1() { //all() /*$students=Student::all(); dd($students) ...

  6. Oracle11g创建表空间及用户

    第1步:创建临时表空间   create temporary tablespace pgenius_temptempfile '/data/oracle/oradata/orcl/pgenius_te ...

  7. 第8课:异常处理、面向对象编程、发送邮件、url编码

    1. 异常处理 import traceback import pymysql import requests def calc(a, b): res = a / b return res def m ...

  8. Linux系统在启动过程中mbr主引导程序被破坏的解决方案

    首先,mbr主引导程序被破坏是指系统在启动过程中,磁头找不到/boot分区(windows的启动分区在c盘). 1)下面我们模拟主引导分区被破坏的情况:(在启动分区划分446M的存储大小) 2)重启( ...

  9. liunx工具学习之taskset

    当你优化多线程任务的时候,发现某个线程在不同的核上跳转较大,从而耗费CPU的时候想法肯定是想可以把对应线程绑定到特定的核上,可是每次这样操作每次尝试看效果的时候都要重启进程,那有没有一个工具可以直接处 ...

  10. linux下利用inode删除指定文件文件

    本文主要介绍使用inode删除异常文件名的文件的方法,供大家参考: 在Linux中,有时候会遇到文件名是乱码或者是某些特殊中文的文件,这时候通过文件名就很难删除. 同时,对于linux中的任何一个文件 ...