LeetCode_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.
/**
* 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:
int countNodeNumb(ListNode *head)
{
int count = ;
while(head)
{
count++;
head = head->next;
} return count ; }
TreeNode * BST(ListNode *head, int size)
{
if(size == ) return new TreeNode(head->val); int half = (size + )/ ; int count = ;
ListNode *pre, *p;
pre = NULL;
p = head;
while(count < half)
{
pre = p;
p = p->next;
count ++ ;
}
TreeNode *Node = new TreeNode(p->val); Node->left = half- > ? BST(head, half-) : NULL ;
Node->right = size - half > ? BST(p->next, size - half) : NULL; return Node ;
}
TreeNode *sortedListToBST(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function if(head == NULL) return NULL ; int size = countNodeNumb(head) ;
TreeNode * myNode = BST(head, size); return myNode;
}
};
LeetCode_Convert Sorted List to Binary Search Tree的更多相关文章
- 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. /* ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] 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 ...
- [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. 这道 ...
- leetcode -- 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 ...
- [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【leetcode】Convert Sorted List to Binary Search Tree
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...
- 【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 ...
- 【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 ...
随机推荐
- Katana 还是Owin ? 本地自承载
使用Owin 将Web项目脱离 IIS确实很特别..... 由此 ,可以衍生出,一个新的通信渠道,本地Server的自承载. 1 Node.js 2 Python 3 Ruby 4 Owin (C#- ...
- WCF的执行过程
既然是实现互通信.那么肯定会有概念意义上的服务端Server 和概念意义上的客户端 Client,在这里,我所说明的都是概念意义上的,单独强调此,是因为,基于WCF的通信没有物理上的划分,但是概念上 ...
- php+jquery+ajax+json的一个最简单实例
html页面: <html> <head> <meta http-equiv="content-type" content="text/ht ...
- ZZY的宠物
Description ZZY领养了一对刚刚出生的不知名小宠物..巨萌巨可爱!!...小宠物的生命为5个单位时间并且不会在中间出意外翘辫子(如: 从0出生能活到5但活不到6)..小宠物经过2个单位时间 ...
- linux下,FTP服务相关
虚拟机上安装完CentOS 6.5后,使用FTP工具(FlashFXP)来上传文件到虚拟机的linux,结果发现连接不上.现在解决了,解决方法总结一下: 1.先检查有没有安装ftp.好像包括两个部分, ...
- MergeSort 归并排序
实现: 二路归并 public class TestMergeSort { public int[] mergeSortArray(int[] arr, int left, int right){ i ...
- java基础 二分查找算法
/* * 折半查找法: * 思路: * 定义三个变量记录查找范围中最大.最小和中间的索引值,每次都是使用中间索引值与要查找的目标进行对比,如果不符合,那么就不停缩小查找范围 * */ ...
- intel线程库tbb的使用
[size=small]首先下载: http://www.threadingbuildingblocks.org/uploads/77/111/2.1/tbb21_20080605oss_win.zi ...
- hdu1015(Safecracker )
Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle ...
- Python进阶之路---1.4python数据类型-数字
python入门基础 声明:以后python代码未注明情况下,默认使用python3.x版本 1.python代码基础:print print('hello,python') 1.1pyt ...