Leetcode 之Convert Sorted List to Binary Search Tree(55)
和上题思路基本一致,不同的地方在于,链表不能随机访问中间元素。
int listLength(ListNode* node)
{
int n = ;
while (node)
{
n++;
node = node->next;
}
return n;
}
ListNode* nth_node(ListNode* node, int n)
{
while (--n)node = node->next;
return node;
}
TreeNode* sortedListToBST(ListNode* head)
{
sortedListToBST(head, listLength(head));
}
TreeNode* sortedListToBST(ListNode* head, int len)
{
if (len == )return nullptr;
if (len == )return new TreeNode(head->val); TreeNode* root = new TreeNode(nth_node(head, len / + )->val);
root->left = sortedListToBST(head, len / );
root->right = sortedListToBST(head, len / + ); return root;
}
Leetcode 之Convert Sorted List to Binary Search Tree(55)的更多相关文章
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode] 109. 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][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【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 ...
- 【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 ...
- Leetcode#109 Convert Sorted List to Binary Search Tree
原题地址 跟Convert Sorted Array to Binary Search Tree(参见这篇文章)类似,只不过用list就不能随机访问了. 代码: TreeNode *buildBST( ...
- leetcode -day19 Convert Sorted List to Binary Search Tree
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted ...
- 37. leetcode 108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)
108. Convert Sorted Array to Binary Search Tree 描述 Given an array where elements are sorted in ascen ...
- LeetCode 108. Convert Sorted Array to Binary Search Tree (将有序数组转换成BST)
108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...
随机推荐
- 国内公共DNS
DNS(Domain Name System,域名系统),因特网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读取的IP数串.通过主机名,最终 ...
- 【OpenCV入门教程之二】OPENCV3 开源之美 — 编译源代码、配置opencv_contrib
为什么要配置opencv_contrib? opencv3.0版本 功能更加模块块,一些功能模块不够完善,等足够完善在merge到主分支中,而我们图像识别中要用到的SIFT等算法被封装在xfeactu ...
- java多线程-Exchanger
简介: 可以在对中对元素进行配对和交换的线程的同步点.每个线程将条目上的某个方法呈现给exchange方法,与伙伴线程进行匹配,并且在返回时接收其伙伴的对象.Exchanger 可能被视为Synchr ...
- Cocos2d-X3.0 刨根问底(七)----- 事件机制Event源码分析
这一章,我们来分析Cocos2d-x 事件机制相关的源码, 根据Cocos2d-x的工程目录,我们可以找到所有关于事件的源码都存在放在下图所示的目录中. 从这个event_dispatcher目录中的 ...
- PostConstruct
Spring AOP注解通过@Autowired,@Resource,@Qualifier,@PostConstruct,@PreDestroy注入属性的配置文件详解 1.6. @PostConstr ...
- SpringMVC中Controller和RestController
项目中的@Controller下有的是返回String类型的(比如getAllBook),有的是void的,当然,String类型是转发的页面,在void中用的是pringwrite,我今天想做一件事 ...
- 【poj3537】 Crosses ans Crosses
poj.org/problem?id=3537 (题目链接) 题意 给出一个1*n的棋盘,每次可以选择一个没被标记过的点打标记,若经过某一步操作使得出现3个连续的标记,则最后操作的人获胜.问是否存在先 ...
- 迪杰斯特拉(Java)
public class Dijsktra { public static void main(String[] args) { Dijsktra d=new Dijsktra(); int[][] ...
- boost解析json
#include <QtCore/QCoreApplication> #include <boost/property_tree/ptree.hpp> #include < ...
- The illustrated guide to a Ph.D.