(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. Mac 下 Chrome 快捷键大全

    1. 标签页和窗口快捷键 ⌘-N 打开新窗口. ⌘-T 打开新标签页. ⌘-Shift-N 在隐身模式下打开新窗口. 按 ⌘-O,然后选择文件. 在 Chrome 浏览器中打开计算机中的文件. 按住  ...

  2. Ignatius and the Princess II(全排列)

    Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ( ...

  3. Ubuntu Code::Blocks IDE 13.12 汉化

    Ubuntu Code::Blocks IDE 13.12 汉化: 安装很简单,不再赘述. 单说汉化: .下载中文简体汉化包(百度网盘):链接: http://pan.baidu.com/s/1kU3 ...

  4. hibernate 简单查询

    1. 查询整个映射对象所有字段 //直接from查询出来的是一个映射对象,即:查询整个映射对象所有字段           String hql = "from Users";   ...

  5. 生成php所需要的APNS Service pem证书的步骤

    1.登录到 iPhone Developer Connection Portal 并点击 App IDs 2.创建一个不使用通配符的 App ID .通配符 ID 不能用于推送通知服务.例如,我们的i ...

  6. android:visibility

    RelativeLayout android:visibility="gone/visible/invisible" 此属性意思是此视图是否显示 例如RelativeLayout中 ...

  7. php测试时不出现错误信息

    来源:http://blog.sina.com.cn/s/blog_6c9d65a101013vdj.html 在练习程序时,有时候写错了,在浏览器会打印出出错信息. 可我的程序始终没有出现. 我的环 ...

  8. java.util.Random 类的 nextInt(int num )

    随机产生3个67~295的整数并找出数值居中的数 并输出中间的数例如:100,225和200,输出200 要随机产生某个范围内的整数,用 java.util.Random 类的 nextInt(int ...

  9. JS中格式化数据保留两位小数

    问题:在JS中格式化数据保留两位小数的函数的多种方法 最好方法: 保留两位好像是这样吧     var   a   =   9.39393;     alert(a.toFixed(2)); 说明: ...

  10. Selenium自动化测试(java语言)

    Selenium介绍 Selenium 1.0 包含 core. IDE. RC. grid 四部分,  selenium 2.0 则是在两位大牛偶遇相互沟通决定把面向对象结构化( OOPP) 和便于 ...