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. Avg_row_length是怎么计算的?

    通过一下命令我们可以获取表的使用情况: root::>show table status like 'tbname'\G 结果: . row ************************** ...

  2. Java数据结构之表的增删对比---ArrayList与LinkedList之一

    一.Java_Collections表的实现 与c不同Java已经实现并封装了现成的表数据结构,顺序表以及链表. 1.ArrayList是基于数组的实现,因此具有的特点是:1.有索引值方便查找,对于g ...

  3. js错误

    1ajax提交 从数据库里取文件typeerror: 'stepup' called on an object that does not implement interface HTMLInputE ...

  4. iOS中如何隐藏启动图片的状态栏

    只隐藏启动图片顶部的状态栏,而不影响程序运行起来控制器的状态栏显示?在info.plist文件中添加一个Statis bar is...... 默认是NO改为YES即可

  5. 两个NetSuite之间历史交易数据迁移的具体方案

    背景与展望: 比如:公司要上市往往会要求提供过去几年的营业数据和报表等信息, 而这些信息来源于正在一直运营使用的ERP和财务系统是最可靠与真实的. NetSuite实现的ERP和财务系统的完美结合,随 ...

  6. android 加载网络图片

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. R语言拆分字符串

    R语言拆分字符串 aaa<-"aa;bb;cc"ccc<-strsplit(aaa,split=";") bbb<- unlist(strsp ...

  8. jquery tmpl 详解

    官方解释对该插件的说明:将匹配的第一个元素作为模板,render指定的数据,签名如下: .tmpl([data,][options]) 其中参数data的用途很明显:用于render的数据,可以是任意 ...

  9. 如何挂自己的web项目(免费拥有自己的网站及域名)

    http://www.blogjava.net/qingshow/archive/2010/01/17/309846.html ------------------------------------ ...

  10. 常见的js 里对数字进行处理的函数方法集合

    常见的对小数值舍入为整数的几个方法:Math.ceil().Math.floor()和Math.round(). 这三个方法分别遵循下列舍入规则: Math.ceil()执行向上舍入,即它总是将数值向 ...