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

Solution:

     TreeNode *sortedListToBST(ListNode *head) {
if(head == NULL)
return NULL;
if(head->next == NULL)
return new TreeNode(head->val); ListNode * current = head;
int size = ;
while(current != NULL){
size ++;
current = current->next;
}
current = head;
int median = size / ;
int count = ;
ListNode * median_node = NULL;
ListNode * pre_node = NULL;
while(current != NULL){
if(count == median){
median_node = current;
pre_node->next = NULL;
break;
}
count ++;
pre_node = current;
current = current->next;
}
TreeNode * root = new TreeNode(median_node->val);
root->left = sortedListToBST(head);
root->right = sortedListToBST(median_node->next);
return root;
}

Convert Sorted List to Binary Search Tree [LeetCode]的更多相关文章

  1. Convert Sorted Array to Binary Search Tree leetcode java

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  2. Convert Sorted Array to Binary Search Tree || LeetCode

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...

  3. Convert Sorted Array to Binary Search Tree——LeetCode

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

  4. Convert Sorted List to Binary Search Tree leetcode java

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

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

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

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

  7. [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. 这道 ...

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

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

随机推荐

  1. oracle表分区、表分析及oracle数据泵文件导入导出开心版

    1.先说oracle表分区是什么吧,这样吧我们来举个桃子,栗子太小,我们就不举了,我们来举个桃子. 你有500万份文件,你要把他存在磁盘上,好嘛,我们就一个文件夹,500万分文件在那儿杵着,我们想找到 ...

  2. 了解FreeRTOS源文件目录结构

    参考文献: Understanding the FreeRTOS directory structure. 从官网下载下来的FreeRTOS源文件包,对于每一个已经移植完成的处理器,都有一个与之对应的 ...

  3. angular-select绑定之后option不能更新问题

    使用ng-option-- http://jsfiddle.net/sseletskyy/uky9m/1/ 以及select中加入自定义指令  convert-to-number .directive ...

  4. js-sort数组排序

    婆婆妈妈,直上代码: <body> <div> sort()对数组排序,不开辟新的内存,对原有数组元素进行调换 </div> <div id="sh ...

  5. 怎样获取Windows平台下SQL server性能计数器值

    转载自工作伙伴Garrett, Helen "SQL Server Performance Counter captures" Capturing Windows Performa ...

  6. webstorm2016.2 for mac 安装

    文件来自斯蒂芬周: http://www.sdifenzhou.com/?p=6941&jdfwkey=jmnzy 附上详细安装步骤:

  7. 一看便知linux下mysql报错ERROR 1044: Access denied for user: '@localhost' to database 'mysql'

    错误信息:ERROR 1044: Access denied for user: '@localhost' to database 'mysql' linux下解决方案: mysql> use ...

  8. 微信web开发者工具

    http://mp.weixin.qq.com/wiki/10/e5f772f4521da17fa0d7304f68b97d7e.html#.E4.B8.8B.E8.BD.BD.E5.9C.B0.E5 ...

  9. Java类的加载

    1.类的加载步骤 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载.连接.初始化三步来实现对这个类的初始化 加载:将class文件读入内存,并为之创建一个Class对象,任何类被使用 ...

  10. 不定长链表队列C语言实现

    #ifndef _CONST_H_#define _CONST_H_ #include <stdio.h>#include <stdlib.h> typedef enum { ...