【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 ascending order, convert it to a height balanced BST.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *sortedListToBST(ListNode *head) { ListNode *p=head;
int len=;
while(p!=NULL)
{
p=p->next;
len++;
} TreeNode * root=buildTree(head,,len-);
return root; } TreeNode *buildTree(ListNode *&node,int left,int right)
{
if(left>right) return NULL; int mid=(left+right)/; TreeNode *leftTree=buildTree(node,left,mid-);
TreeNode *root=new TreeNode(node->val);
node=node->next;
TreeNode *rightTree=buildTree(node,mid+,right);
root->left=leftTree;
root->right=rightTree;
return root;
} };
【leetcode】Convert Sorted List to Binary Search Tree的更多相关文章
- 【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】Convert Sorted Array to Binary Search Tree (easy)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序 ...
- 【leetcode】Convert Sorted List to Binary Search Tree (middle)
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- 【题解】【BST】【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】【Medium】Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题 ...
- 【树】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] 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 OJ】Convert Sorted Array to Binary Search Tree
Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...
随机推荐
- Java序列化的几种方式以及序列化的作用
Java序列化的几种方式以及序列化的作用 本文着重讲解一下Java序列化的相关内容. 如果对Java序列化感兴趣的同学可以研究一下. 一.Java序列化的作用 有的时候我们想要把一个Java对象 ...
- CocoaLumberjack
刚学iOS时候,调试找问题的时候一般就两种方式. 1.输出NSLog日志. 2.打断点,一步一步查看问题,查找崩溃点所在的方法,再进一步查看崩溃具体原因,稍微高级一点的就在控制台用命令打印各种参数. ...
- 【转】asp.net mvc 页面跳转
1.使用传统的Response.Redirect例如string url = "/account/create";Response.Redirect(url); 1.Server. ...
- 新浪微博客户端(36)-自定义带placeholder的TextView
iOS 上自带的UITextView竟然不能设置placeholder,但是UITextView却可以,我也真是醉了.没办法了,自己写一个 DJTextView.h #import <UIKit ...
- 新浪微博客户端(34)-block的细节与本质
main.m #import <Foundation/Foundation.h> void test4(); int main(int argc, const char * argv[]) ...
- 系统研究Airbnb开源项目airflow
开源项目airflow的一点研究 调研了一些几个调度系统, airflow 更满意一些. 花了些时间写了这个博文, 这应该是国内技术圈中最早系统性研究airflow的文章了. 转载请注明出处 htt ...
- 利用CSS实现带相同间隔地无缝滚动动画
说明:因为在移动上主要利用CSS来做动画,所以没有考虑其他浏览器的兼容性,只有-webkit这个前缀,如果需要其他浏览器,请自行补齐. 首先解释一下什么是无缝滚动动画, 例如下面的例子 See the ...
- 清北暑假模拟day1 生活
/* 数字三角形,要求第K大的值,可以推知,如果得知k的范围,那么一定是在上一行可转移状态的对应范围内(反证法可以证明),这个在背包九讲里也有提及 */ #include<cstdio> ...
- 利用百度语音API进行语音识别。
由于项目需要,这几天都在试图利用百度语音API进行语音识别.但是识别到的都是“啊,哦”什么的,我就哭了. 这里我只是分享一下这个过程,错误感觉出现在Post语音数据那一块,可能是转换问题吧. API请 ...
- eclipse如何导入java项目文件
平时下载到项目时,希望能够导入到eclipse中使用.但有些项目不能直接导入,需要做转换. 打开源文件目录,查询如下: 如果目录中包含pom.xml文件,则说明该项目由Maven构建的,参考以下 如何 ...