Convert Sorted List to Balanced Binary Search Tree (BST)
(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)的更多相关文章
- 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 ...
- Convert Sorted List to Balanced Binary Search Tree leetcode
题目:将非递减有序的链表转化为平衡二叉查找树! 参考的博客:http://blog.csdn.net/worldwindjp/article/details/39722643 利用递归思想:首先找到链 ...
- 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 ...
- 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 ...
- UVA 1264 - Binary Search Tree(BST+计数)
UVA 1264 - Binary Search Tree 题目链接 题意:给定一个序列,插入二叉排序树,问有多少中序列插入后和这个树是同样的(包含原序列) 思路:先建树,然后dfs一遍,对于一个子树 ...
- Codeforces 1237E Perfect Balanced Binary Search Tree
题目链接 Observations 含有 $n$ 个点且 key(以下也称 key 为「权值」)是 1 到 $n$ 的 BST 具有下列性质: 若 $k$ 是一个非根叶子且是个左儿子,则 $k$ 的父 ...
- 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 ...
- Binary Search Tree BST Template
Use one queue + size variable public class Solution { public ArrayList<ArrayList<Integer>&g ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
随机推荐
- POJ3322-经典的游戏搜索问题
临近毕业真是各种琐事多,好不容易写完几万字蛋疼的论文,又接着户口档案挂靠,毕业旅游,20多个离校盖章,签证被check了几个星期还没消息,希望8月初能走啊. 各种事情之下,人就是懒加心散,好久没写代码 ...
- C# NameValueCollection集合 .
案例: NameValueCollection nameValueCollection = Request.Params;//获得连接地址中的所有参数 //获取各个参数,eg: ...
- oracle tablespace
oracle tablespace 1. 查看所有表空间大小 SQL> select tablespace_name,sum(bytes)/1024/1024 from dba_data_fil ...
- 剑指offier第4题
/* 问题1:替换字符串,是在原来的字符串上做替换,还是新开辟一个字符串做替换! 问题2:在当前字符串替换,怎么替换才更有效率(不考虑java里现有的replace方法). 从前往后替换,后面的字符要 ...
- Linux 环境变量和source命令 (转)
可能是班门弄斧了,仅share给尚不知道的童鞋. 1. 问题的来源: 为什么我们编译Android代码时,需要输入: source ./build/envsetup.sh 或者 . . ...
- [一道搜狗输入法的面试题]C++转换构造函数和类型转换函数
今天面试遇到一道有关C++转换构造函数的题目,之前经常见到默认构造函数.拷贝构造函数.析构函数,但是从没听说过转换构造函数,隐式转换函数也是一样,C++的确是够博大精深的,学习之路很长啊! 其实我们已 ...
- mysql学习(四)-字段类型
mysql数据类型: 数值型: 整形:int 浮点型:float double decimal:定点型 日期: date '2012-01-02' time '10:01:01' datetime ...
- <s:if>标签与ActionContext.getContext().getSession()
今天在做<s:if>标签中的属性值从 ActionContext.getContext().getSession().put("WW_TRANS_I18N_LOCALE" ...
- [置顶] 蓝牙基础知识进阶——Physical channel
从本篇文章开始,晓东将会和大家一起来学习一些蓝牙的比较高阶的基础知识. 二.物理通道 物理通道是piconet区分的标准,它是蓝牙系统结构层次中的最底层了. Q1:物理通道有哪些类型 物理通道 ...
- SilverlightLoader使用托管代码创建自定义载入界面及动态加载XAP
Silverlight实现动态加载xap和Splash Screen.收藏! 内容来自 http://silverlightchina.net/html/tips/2010/0115/588.html