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

```/**
* 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:
{
int count = ;
{
count++;
}

return count ;

}
TreeNode * BST(ListNode *head, int size)
{
if(size == ) return  new TreeNode(head->val);

int half = (size +  )/ ;

int count = ;
ListNode *pre, *p;
pre = NULL;
while(count < half)
{
pre = p;
p = p->next;
count ++ ;
}
TreeNode *Node = new TreeNode(p->val);

Node->left = half- >  ? BST(head, half-) : NULL ;
Node->right = size - half > ? BST(p->next, size - half) : NULL;

return Node ;
}
// Start typing your C/C++ solution below
// DO NOT write int main() function

if(head == NULL) return NULL ;

TreeNode * myNode = BST(head, size);

return myNode;
}
};```

LeetCode_Convert Sorted List to Binary Search Tree的更多相关文章

1. LeetCode_Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /* ...

2. Leetcode: Convert sorted list to binary search tree (No. 109)

Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

3. [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 ...

4. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

5. 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 ...

6. [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

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

8. 【leetcode】Convert Sorted Array to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

9. 【LeetCode OJ】Convert Sorted Array to Binary Search Tree

Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...

随机推荐

2. java基础之 超类Object

一.概述:       Object类是所有Java类的祖先.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个类的方法.在不明确给出超类的情况下,Java会自动把Object作为要 ...

3. Centos7网络配置,vsftpd安装及530报错解决

今天在虚拟机安装CentOS7,准备全新安装LTMP,结果又是一堆问题,不过正好因为这些出错,又给自己长了见识. 1,CentOS7网络配置 最小化安装CentOs7后,ifconfig提示comma ...

4. Spring源码追踪2——xml解析入口

5. Cloud Insight支持阿里云一键接入了，so what?

前几天 Cloud Insight 上线了一个新功能,考虑到目前只作为公测,所以只是是悄悄地加了一个接入项,希望你看完这偏文章会有兴趣体验一下. 相信体验过的用户(目前还是个位数)第一感受应该是:这个 ...

6. 转载：邮箱正则表达式Comparing E-mail Address Validating Regular Expressions

7. CSS选择符 新的认识

以前写选择符,很少使用">"符号,一般都是使用" "空格来代替.我觉得他们都是一样的功能,另一方面是为了方便.毕竟不用按住shift. 例如: .demo ...

8. SQL数据库开发中的一些经典代码

1.按姓氏笔画排序: Select * From TableName Order By CustomerName Collate Chinese_PRC_Stroke_ci_as  2.数据库加密: ...

9. WPF应用

代码 private void button1_Click(object sender, RoutedEventArgs e) { calculate sa = new calculate(int.P ...

10. netscope-支持caffe的在线可视化工具-转载

Netscope是个支持prototxt格式描述的神经网络结构的在线可视工具,地址是here,可以用来可视化Caffe结构里prototxt格式的网络结构. Netscope使用起来也非常简单,打开这 ...