【Lintcode】106.Convert Sorted List to Balanced BST
题目:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
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的更多相关文章
- 【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. ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【原创】leetCodeOj ---Convert Sorted List to Binary Search Tree 解题报告
原题地址: https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题目内容: Given a sing ...
- 【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 ...
- 【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 ...
- 【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 ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- Convert Sorted List to Balanced BST
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- OS开发之旅之App的生命周期【转载】
原文链接 http://www.360doc.com/content/15/0918/14/27799428_499912639.shtml 在iOS App中,入口函数并不在根目录下,而是在“Sup ...
- C++常用强制类型转换
1.static_cast 最常用的类型转换符,在正常状况下的类型转换,如把int转换成float,如: int i; float f; f=(float)i; 或者 f=static_cast(i) ...
- 给定一颗完全二叉树,给每一层添加上next的指针,从左边指向右边
给你机会发出声音,但是不给你机会证明高层的决定是错的 RT: 时间复杂度O(n) 空间复杂度O(1) 原理就是有指针指向父节点和当前的节点,左孩子必指向右孩子,右孩子必指向父节点的下一个节点的左孩子 ...
- Top 10 Open Source Bug Tracking System系统
Bugzilla http://www.bugzilla.org/ Mantis php http://www.mantisbt.org/ Trac Python also provides wiki ...
- C - The C Answer (2nd Edition) - Exercise 1-1
/* Run the "hello, world" program on your system. Experiment with leaving out parts of the ...
- iOS block-base 动画简单用法+关键帧动画设置线性变化速度的问题
本文转载至 http://www.tuicool.com/articles/aANBF3m 时间 2014-12-07 20:13:37 segmentfault-博客原文 http://segm ...
- rtsp转rtmp、hls网页直播服务器EasyNVR前端兼容性调试:ie下的 pointer-events- none
发现问题: 之前在做EasyNVR 的web页面开发过程中,力求的都是一个播放效果的.功能的展示.对于兼容性也有注意,但有些细节还是难免有所疏忽. 内部测试发现:由于我们是流媒体的实时视频直播,在we ...
- 3行代码 多元线性方程组 rank=4 多元-一元 降元
对于线性方程组Ax=b 对A和b执行同样的一串行初等运算, 那么该方程组的解集不发生变化. [未知-已知 高阶--低阶] http://mathworld.wolfram.com/CramersR ...
- FI 常用表
FI 常用表 GL部分:FAGLFLEXT(FMGLFLEXT) 总账汇总表 GLT0 旧总帐汇总表 SKA1 总账科目主记录 (科目表) ...
- xutils3基本使用
根目录下新建一个类继承application,调用xUtils3初始化方法 public class AtguiguApplication extends Application { @Overrid ...