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 ...
随机推荐
- node不懂的方法的使用
1. 学习的时候注意,过滤器,上传文件sftp,读取excel,还有cookie的操作,sql的操作.node的框架express koa hapi 还有引擎ejs,hbs,jade,日志管理等,并发 ...
- [CH#58解题报告]
题目:http://206.contesthunter.org/contest/CH%20Round%20%2358%20-%20OrzCC%E6%9D%AFnoip%E6%A8%A1%E6%8B%9 ...
- Source Tree for MAC1.6
Atlassian ID has become the new Atlassian Account. Read more about it here. After some great communi ...
- Linux 下SVN服务器搭建
系统环境 RHEL5.4最小化安装(关iptables,关selinux) + ssh + yum 一,安装必须的软件包. yum install subversion (SVN服务器 ...
- Daily Scrum – 1/7
Meeting Minutes 搞定了一个bug,单词面板滚动条的bug: 在电脑屏幕上的屏幕适配有了新思路: Progress part 组员 今日工作 Time (h) 明日计划 Time ( ...
- java多线程-CountDownLatch
简介 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.用给定的计数 初始化 CountDownLatch.由于调用了 countDown() 方法,所以在当前计数 ...
- Struts2+Jquery实现ajax并返回json类型数据
来源于:http://my.oschina.net/simpleton/blog/139212 摘要 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的 ...
- zabbix 的学习应用之路
1.zabbix server的安装 http://www.cnblogs.com/smail-bao/p/5643136.html 2.zabbix agent的安装 h ...
- tomcat配置和优化
转载: https://mp.weixin.qq.com/s?__biz=MzA3MzYwNjQ3NA==&mid=2651296654&idx=1&sn=b04fc6cecf ...
- 【Gym 100712B】Rock-Paper-Scissors
题 题意 对给定的对手的出拳顺序,如果只能按几个R,然后几个P,再几个S的顺序出拳(几个也可以是0个),那么求赢的方法有多少种. 分析 我原来想枚举P开始的位置和S开始的位置然后算得分,但是超时了o( ...