Convert Sorted List to Binary Search Tree [LeetCode]
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]的更多相关文章
- 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 ...
- Convert Sorted Array to Binary Search Tree || LeetCode
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...
- 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. 题目 ...
- 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 ...
- 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 Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
- 【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 ...
- 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 ...
随机推荐
- GPIO口及中断API函数【转】
本文转载自:http://blog.sina.com.cn/s/blog_a6559d9201015vx9.htmlG #include <linux/gpio.h> // 标准 GPIO ...
- JSON对象转换问题
今天调用别人接口遇到一个问题,原本约定的data格式为:JSON字符串,但本次返回了一个空字符串"", 大概是这样的 字符串类型的httpResult.data值为字result: ...
- 使用discovery板上的st-link给别的板子下载
discovery板上的6pin swd接口 20pin 的jtag 接线: 6 20 def 1 1 目标vdd 2 9 swclk(PA14) 3 20 gnd 4 7 swdio(PA13) ...
- MySQL int(11)及int(M)解析
默认创建int类型的字段,SHOW CREATE TABLE table_name或DESC table_name常常可以看到其默认情况为int(11). 这个int(M)很多时候都会被误解为最大范围 ...
- Java读取Level-1行情dbf文件极致优化(2)
最近架构一个项目,实现行情的接入和分发,需要达到极致的低时延特性,这对于证券系统是非常重要的.接入的行情源是可以配置,既可以是Level-1,也可以是Level-2或其他第三方的源.虽然Level-1 ...
- Java进行post和get传参数
http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html get和post方法 import java.io.BufferedRe ...
- css样式增加&改变颜色
.listyle{ color:blue; background-color:red /* background-color:"#006633" */ } .intro{ font ...
- NiosII常用函数整理
NiosII常用函数整理 IO操作函数函数原型:IORD(BASE, REGNUM) 输入参数:BASE为寄存器的基地址,REGNUM为寄存器的偏移量函数说明:从基地址为BASE的设备中读取寄存器中偏 ...
- 渗透测试工具Nmap从初级到高级使用教程
本文由阿德马翻译自国外网站,请尊重劳动成果,转载请注明出处,谢谢 Nmap是一款网络扫描和主机检测的非常有用的工具.Nmap是不局限于仅仅收集信息和枚举,同时可以用来作为一个漏洞探测器或安全扫描器.它 ...
- SQL-Server使用点滴(二)
二,对象的建立和使用 1,了解MSSql的[系统表] 对于SQL-Server中的所有对象,包括数据库,数据表,记录,字段,触发器,索引,数据类型等元素,均有对应的系统表记性记录.系统表是禁止直接删改 ...