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 ...
随机推荐
- IC卡接口芯片TDA8007的读写器设计
摘要:阐述T=0传输协议,给出IC卡读写器中使用的IC卡APDU指令流程和原理框图:重点介绍其中的IC卡接口芯片Philips的TDA8007,给出通过TDA8007对CPU IC卡上下电过程.具体程 ...
- java常量设置的方式
我们在写java程序的时候,常常有常量设置,如: public interface Const { //性别的常量 public interface Sex{ public final int 男=1 ...
- hdu1573:数论,线性同余方程组
题目大意: 给定一个N ,m 找到小于N的 对于i=1....m,满足 x mod ai=bi 的 x 的数量. 分析 先求出 同余方程组 的最小解x0,然后 每增加lcm(a1...,am)都 ...
- C++基础回顾1(数据类型, 控制语句, 数组)
最近两天打开本科学校的C++教材,快速回顾了一下C++方面的内容.虽然书本内容比较基础,但是还是有些知识点值得自己强化记忆.分几篇文章,加上自己的理解记录如下. 先回顾面向过程的部分. C++数据类型 ...
- 基于PCA的特征提取
图像处理方面的知识也学了一段时间了,总是光看理论的话,感觉联系不上实际,第一次把理论综合的实现出来,对这些理论的印象才感觉的更深刻,也能够为后续的学习打下良好的基础. PCA是比较老的算法,但是可靠性 ...
- hdu 5423 Rikka with Tree(dfs)
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...
- hdu 5072 Coprime (容斥)
Problem Description There are n people standing in a line. Each of them has a unique id number. Now ...
- php之手机号码查归属地
免费手机号码归属地API查询接口 一.淘宝网API API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=1585078144 ...
- vbox下centos安装增加功能失败
一般都是:unable to find the sources of your current Linux kernel. 先尝试这个吧:yum install kernel kernel-heade ...
- (转载)XML Tutorial for iOS: How To Read and Write XML Documents with GDataXML
In my recent post on How To Choose the Best XML Parser for Your iPhone Project, Saliom from the comm ...