(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. HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一个带边权的图.对于每一个询问(S , ...

  2. L6 Shell

    写出以下shell脚本1 判定一个用参数指定的文件是否为可执行,如果不是,则改为可执行#!/bin/bash if [ -x $1 ]then echo "OK"else echo ...

  3. iOS 自定义button

    UIButton默认的布局是左侧image,右侧title,如果想要改变image与title的frame,直接设置是不会有效果的.可以通过titleEdgeInsets.imageEdgeInset ...

  4. inline-block代替浮动布局float:left列表布局最佳方案

    基于各位前辈的辛勤劳动,下面得出使用inline-block替换float:left;的最佳方案. html代码 <div class="list"> <ul&g ...

  5. jQuery获取Select选择的Text和Value(详细汇总)

    语法解释: 1. $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 2. var ch ...

  6. mysql模糊匹配

    select * from tableName where column like ""; select * from tableName where column regexp ...

  7. java面向对象之 封装 Encapsulation

    什么是封装:对象中的成员该隐藏的隐藏.该公开的要公开 封装:顾名思义,隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别:将抽象得到的数据和行为(或功能)相结合,形成一个有 ...

  8. 【Howie玩docker】-windows下玩docker

    Windows下安装toolbox一直没成功,于是投机取巧,用虚拟机手工打造玩docker的方法. 步骤: 安装虚拟机,安装centos 在win下建立共享文件夹,假如是 f:/share 在cent ...

  9. 编写一个程序实现strcat函数的功能

    写自己的strcat函数------→mycat #include <stdio.h> #include <string.h> #define N 5 char *mycat( ...

  10. 轮播图插件myFocus使用

    myFocus官网下载源码,本文是v2.0.1版,解压后如下 将js包内文件拷入工程 在工程内引入 <script src="js/myfocus-2.0.1.min.js" ...