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 ...
随机推荐
- 《编写可维护的JavaScript》——JavaScript编码规范(三)
啦啦啦啦啦,今天第二篇随笔\(^o^)/~ ////////////////////////////////正文分割线////////////////////////////////////// 直接 ...
- 分页sql优化
如果分页sql里包含排序: select * from (...order by id) where rownum <=20 因为要排序,所以即使是分页只取20条,执行计划还是要把所有满足条件的 ...
- 2016湖大校赛 L题 The Sequence likes Ladder
题意:S1=a,Sn=a*(Sn-1)^k%m,且有(a,m)=1,给出i,求Si. 思路:首先我们可以写出Sn的通项a^(1+k+k^2+...k^n-1);其次注意到m的范围是10000以内,所以 ...
- easyui DataGrid 工具类之 TableUtil class
import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.HashM ...
- 完整安装cocoaPods
cocoaPods是一款xcode项目管理第三方库的工具 *ruby源码镜像下载:https://ruby.taobao.org/mirrors/ruby/*升级gem的版本: $sudo gem u ...
- 贪吃蛇,JavaScript,效果,鼠标事件
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>鼠 ...
- docker里重装mysql
1.查看ubuntu下装了什么软件: dpkg -l 2.删除mariadb: apt-get autoremove --purge mariadb-server-10.0 apt-get remov ...
- Css绘制圆形,环形,椭圆等图形
转载自http://blog.csdn.net/gongstrong123/article/details/50888758 绘制圆形,环形,椭圆 <!DOCTYPE html> < ...
- 为listview的item添加动画效果
//动画集合 AnimationSet animationSet = new AnimationSet(true); //alpha动画 Animation animation = new Alpha ...
- Query Designer:变量的偏移 Variable Offset
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...