[Linked List]Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
/**
* 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:
int getListNodeLength(ListNode* head)
{
int len = ;
while(head){
len++;
head = head->next;
}
return len;
}
TreeNode* sortedListToBST(ListNode* head) {
if(head==NULL){
return NULL;
}
if(head->next==NULL){
return new TreeNode(head->val);
}
int len = getListNodeLength(head);
int cnt = len/; ListNode* cur = head;
while(--cnt){
cur = cur->next;
}
ListNode* mid = cur->next;
ListNode* head2= mid->next;
cur->next = NULL;
mid->next = NULL; TreeNode* root = new TreeNode(mid->val);
root->left = sortedListToBST(head);
root->right = sortedListToBST(head2);
return root;
}
};
[Linked List]Convert Sorted List to Binary Search Tree的更多相关文章
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 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 ...
- 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 ...
- [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【leetcode】Convert Sorted List to Binary Search Tree
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...
- 34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree
Convert Sorted List to Binary Search Tree OJ: https://oj.leetcode.com/problems/convert-sorted-list-t ...
- LeetCode: Convert Sorted List to Binary Search Tree 解题报告
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...
- LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree
LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...
- leetcode -day19 Convert Sorted List to Binary Search Tree
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted ...
随机推荐
- 解决iOS9苹果将原http协议改成了https协议问题
解决方法: 在info.plist 加入key <key>NSAppTransportSecurity</key> <dict> <key>NSAllo ...
- UIScrollView 滑动试图
UIScrollView --->UIView //创建UIScrollView testScrollView=[[UIScrollView alloc]init]; testScrollVie ...
- 已知TSP问题的最好解
a280 : 2579ali535 : 202339att48 : 10628att532 : 27686bayg29 : 1610bays29 : 2020berlin52 : 7542bier12 ...
- Cortex-M3知识点
1.不再像别的ARM7那样从thumb状态和ARM状态来回切换 Thumb-2指令集横空出世,Cortex-M3不支持ARM指令集 2.BKP备份寄存器(42个16位寄存器组成),用来存储用户应用程序 ...
- CDZSC_2015寒假新人(2)——数学 P
P - P Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- full-background.js
$(window).on('load', function () { $(window).trigger('resize'); }); $(function () { var $window = $( ...
- hibernate安装和配置和第一个Hibernate应用
ssh的各个版本要搭配好,struts2.3一般搭配hibernate4.x,我下的是4.3. Hibernate4的改动较大只有spring3.1以上版本能够支持,Spring3.1取消了Hiber ...
- 2014第3周六升级win8.1
今天上班主要是沟通了一个需求,然后思考下实现方案并记录下要点,晚上没有加班就回来,把操作系统升级到了win8.1,升级的重要原因是,原来的win7时常会卡顿,并且开关机慢,还有上面装了很多无用的影响效 ...
- 给ie浏览器添加推荐浏览器提示
<script type="text/javascript"> var isIE = !!window.ActiveXObject; var isIE6 = isIE ...
- Trapping Raining Water 解答
Question Given n non-negative integers representing an elevation map where the width of each bar is ...