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.

 
 
可以采用类似于Covert Sorted Array to Binary Search Tree的方法,但是寻找中点对于链表来说效率较低
可以采用更高效的递归方式,无需寻找中点
注意引用传递:TreeNode *buildTree(ListNode *&node,int left,int right)
 
 /**
* 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的更多相关文章

  1. 【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 ...

  2. 【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. 有序 ...

  3. 【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 ...

  4. 【题解】【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.思路: ...

  5. 【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. 解题 ...

  6. 【树】Convert Sorted Array to Binary Search Tree

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  7. [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 ...

  8. [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 ...

  9. 【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 ...

随机推荐

  1. Spring的Bean之Bean的基本概念

    从前面我们知道spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...

  2. python- shutil 高级文件操作

    简介 shutil模块提供了大量的文件的高级操作.特别针对文件拷贝和删除,主要功能为目录和文件操作以及压缩操作.对单个文件的操作也可参见os模块. 拷贝文件 shutil.copyfile(src, ...

  3. Windows系统bug

    今天,发现Windows系统的一个bug 也不知道是不是bug,未深入 在网上下载图片,将图像另存为到本地的时候,图片文件名可以为空(后缀要保留) 但是,在本地,是无法直接将文件名命名为空的~

  4. 整理一下Entity Framework的查询

    整理一下Entity Framework的查询 2012-08-30 13:41:59 标签:Entity Framework 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信 ...

  5. js的DOM对象

    1.js的Array对象           ** 创建数组(三种)                          - var arr1 = [1,2,3];                    ...

  6. MVC缓存OutPutCache学习笔记 (二) 缓存及时化VaryByCustom

    <MVC缓存OutPutCache学习笔记 (一) 参数配置> 本篇来介绍如何使用 VaryByCustom参数来实现缓存的及时化.. 根据数据改变来及时使客户端缓存过期并更新.. 首先更 ...

  7. PHP笔记

    <?php//统计访问量 if(!@$fp=fopen("num.txt","r")){ echo "num.txt文件创建成功!<br& ...

  8. javascript取一周的日期

    上代码: <script> var today = new Date(); for (var i = 0; i < 7; i++) { today.setDate(today.get ...

  9. 贴上自己写的代码-jq隐藏事件

  10. Codeforces Round #270 1002

    Codeforces Round #270 1002 B. Design Tutorial: Learn from Life time limit per test 1 second memory l ...