题目:

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

Example

               2
1->2->3 => / \
1 3

 

题解:

Solution 1 ()

class Solution {
public:
TreeNode *sortedListToBST(ListNode *head) {
if (!head) return nullptr; return sortedListToBST(head, nullptr);
}
TreeNode *sortedListToBST(ListNode* head, ListNode* tail) {
if (head == tail) return nullptr;
ListNode* mid = head, *tmp = head; while (tmp != tail && tmp->next != tail) {
mid = mid->next;
tmp = tmp->next->next;
}
TreeNode* root = new TreeNode(mid->val);
root->left = sortedListToBST(head, mid);
root->right = sortedListToBST(mid->next, tail); return root;
} };

Solution 2 ()

class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
if (head == nullptr)
return nullptr;
ListNode* fast = head;
ListNode* slow = head;
ListNode* prev = nullptr;
while (fast != nullptr && fast->next != nullptr)
{
fast = fast->next->next;
prev =slow;
slow = slow->next;
}
TreeNode* root = new TreeNode(slow->val);
if (prev != nullptr)
prev->next = nullptr;
else
head = nullptr; root->left = sortedListToBST(head);
root->right = sortedListToBST(slow->next); return root;
} };

【Lintcode】106.Convert Sorted List to Balanced BST的更多相关文章

  1. 【Lintcode】177.Convert Sorted Array to Binary Search Tree With Minimal Height

    题目: Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. ...

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

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

  3. 【原创】leetCodeOj ---Convert Sorted List to Binary Search Tree 解题报告

    原题地址: https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题目内容: Given a sing ...

  4. 【easy】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 ...

  5. 【LeetCode】108. Convert Sorted Array to Binary Search Tree

    Problem: Given an array where elements are sorted in ascending order, convert it to a height balance ...

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

  7. 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  8. Convert Sorted List to Balanced BST

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

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

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

随机推荐

  1. python3 configparser对配置文件读写

    import configparser #read data from conf filecf=configparser.ConfigParser()cf.read("biosver.cfg ...

  2. FTP匿名登录或弱口令漏洞及服务加固

    漏洞描述 FTP 弱口令或匿名登录漏洞,一般指使用 FTP 的用户启用了匿名登录功能,或系统口令的长度太短.复杂度不够.仅包含数字.或仅包含字母等,容易被黑客攻击,发生恶意文件上传或更严重的入侵行为. ...

  3. python 安装protobuf

    安装准备:python和protoc(编译proto到各个语言) 下载protobuf源代码(各种语言实现):https://github.com/google/protobuf  1.到Python ...

  4. View数据呈现相关技术

    一.了解Razor语法 1.Razor基本语法 a)输出单一变量时不需要加分号做结尾.如: <p>现在时刻:@DateTime.Now</p> b)输出一段含有空白字元或运算子 ...

  5. Log4j2升级jar包冲突问题

    升级Log4j2后日志提示jar包冲突: SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar ...

  6. 深入详解WPF ControlTemplate

    WPF包含数据模板和控件模板,其中控件模板又包括ControlTemplate和ItemsPanelTemplate,这里讨论一下WPF ControlTemplate. 其实WPF的每一个控件都有一 ...

  7. php生成唯一的串

    1.方法一: <?php md5(uniqid('aa',true)); ?> 2.方法2: //生成16位的串$randLength=6; $chars='abcdefghijklmno ...

  8. 【BZOJ4241】历史研究 分块

    [BZOJ4241]历史研究 Description IOI国历史研究的第一人——JOI教授,最近获得了一份被认为是古代IOI国的住民写下的日记.JOI教授为了通过这份日记来研究古代IOI国的生活,开 ...

  9. UITableView的headerView和headerInsectionView

    UITableView有两个headerView:tableHeaderView.和headerInsectionView(组头视图).   给tableView添加这两个View:tableHead ...

  10. Cauchy sequence Hilbert space 希尔波特空间的柯西序列

    http://mathworld.wolfram.com/HilbertSpace.html A Hilbert space is a vector space  with an inner prod ...