(http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html)

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

Code:

BinaryTree* sortedListToBST(ListNode *& list, int start, int end)
{
if (start > end)
return NULL;
int mid = start + (end - start) / ;
BinaryTree* leftChild = sortedListToBST(list, start, mid-);
BinaryTree* parent = new BinaryTree(list->data);
parent->left = leftChild;
list = list->next;
parent->right = sortedListToBST(list, mid+, end);
return parent;
} BinaryTree* sortedListToBST(ListNode* head, int n)
{
return sortedListToBST(head, , n-);
}

Convert Sorted List to Balanced Binary Search Tree (BST)的更多相关文章

  1. Convert Sorted Array to Balanced Binary Search Tree (BST)

    (http://leetcode.com/2010/11/convert-sorted-array-into-balanced.html) Given an array where elements ...

  2. Convert Sorted List to Balanced Binary Search Tree leetcode

    题目:将非递减有序的链表转化为平衡二叉查找树! 参考的博客:http://blog.csdn.net/worldwindjp/article/details/39722643 利用递归思想:首先找到链 ...

  3. Lowest Common Ancestor of a Binary Search Tree (BST)

    Given a binary search tree(BST), find the lowest common ancestor of two given nodes in the BST. Node ...

  4. PAT 1099 Build A Binary Search Tree[BST性质]

    1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary ...

  5. UVA 1264 - Binary Search Tree(BST+计数)

    UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...

  6. Codeforces 1237E Perfect Balanced Binary Search Tree

    题目链接 Observations 含有 $n$ 个点且 key(以下也称 key 为「权值」)是 1 到 $n$ 的 BST 具有下列性质: 若 $k$ 是一个非根叶子且是个左儿子,则 $k$ 的父 ...

  7. Convert Binary Search Tree (BST) to Sorted Doubly-Linked List

    (http://leetcode.com/2010/11/convert-binary-search-tree-bst-to.html) Convert a BST to a sorted circu ...

  8. Binary Search Tree BST Template

    Use one queue + size variable public class Solution { public ArrayList<ArrayList<Integer>&g ...

  9. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

随机推荐

  1. Mysql两种存储引擎的优缺点

    MyISAM引擎是一种非事务性的引擎,提供高速存储和检索,以及全文搜索能力,适合数据仓库等查询频繁的应用.MyISAM中,一个table实际保存为三个文件,.frm存储表定义,.MYD存储数据,.MY ...

  2. 在Oracle中使用sql获取数据库名称

    在Oracle中使用sql获取当前数据库名称 select name from v$database;

  3. MySQL Troubleshoting:Waiting on query cache mutex

    今天被MySQL Query Cache 炕了.线上大量 Waiting on query cache mutex 那么什么是 Query Cache? QC 缓存的是整个SELECT的结果集.而非执 ...

  4. .NET,你忘记了么?(八)—— 从dynamic到特性误用 [转]

    1. 摘要 每个程序员都想写出漂亮的代码,但是什么是漂亮,这个我想每个人都有着自己的看法.那么我就说几种典型的想法: A. 写出别人看不懂的代码,让别人觉得很高深. B. 写出简短的代码 C. 用最新 ...

  5. lightoj 1030

    递推,倒着递推. #include<stdio.h> #define maxn 1010 #define min(a,b) (a)>(b)?(b):(a) int main() { ...

  6. velocity 字符串 转化为数字

    #set($nPageIndex=$request.getParameter("nIndex")) #set($Integer = 0 ) #set($FrontPageSize= ...

  7. 定时自动备份mysql数据库

    新建备份文件并赋予可以执行的权限 mkdir -p /home/mysql_backup/ touch /home/mysql_backup/mysql_backup.sh chmod 551 /ho ...

  8. Activity之间定时跳转

    起源:很多应用在打开时,首先会加载欢迎页面,经过几秒后再跳转到主页面. 下面,我通过两种不同的方式来实现页面的定时跳转. 第一种方式: 通过Timer类的schedule方法. 实现从MainActi ...

  9. Table的分割线偏移量设置 及其 UIEdgeInset详解

    -(void)viewDidLayoutSubviews { if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)] ...

  10. qutIm编译

    官网:http://www.qutim.org/ 原文地址:http://wiki.qutim.org/en/building_from_git 依赖: Qt4-dev 4.7:http://qt-p ...