[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.
题意:给定一个有序的链表,将其转换成平衡二叉搜索树
思路: 二分法
要构建一个平衡二叉树,二分法无疑是合适的,至于如何分是的代码简洁,就需要用到递归了。
- class Solution {
- public:
- // find middle element of the list
- ListNode *getmiddleList(ListNode *left,ListNode *right){
- //omit the condition : left!=right && left->next!=right
- ListNode *pre,*last;
- pre=left; last =left->next;
- while(last!=right){
- last = last->next;
- if(last!=right){
- last = last->next;
- pre=pre->next;
- }
- }
- return pre;
- }
- // retri-BST constructor
- TreeNode *getBST(ListNode *left,ListNode *right){
- TreeNode *root = new TreeNode();
- //no leaf
- if(left==right) return NULL;
- // only one leaf
- if(left->next == right){
- root->val=left->val;
- return root;
- }
- //more than one leaf
- ListNode *middle =getmiddleList(left,right);
- root->val = middle->val;
- root->left = getBST(left, middle);
- root->right = getBST(middle->next,right);
- return root;
- }
- TreeNode *sortedListToBST(ListNode *head) {
- TreeNode* root= new TreeNode();
- if(head==NULL) return NULL;
- if(head->next==NULL){
- root->val=head->val;
- root->left=root->right=NULL;
- return root;
- }
- ListNode *left,*middle,*right;
- middle=left=head;
- right=head->next;
- while(right){
- right=right->next;
- if(right){
- right=right->next;
- middle=middle->next;
- }
- }
- root->val=middle->val;
- root->left = getBST(left, middle);
- root->right= getBST(middle->next,right);
- return root;
- }
- };
转载请注明出处:http://www.cnblogs.com/double-win/ 谢谢!
[LeetCode 题解]:Convert Sorted List to Binary Search Tree的更多相关文章
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [LeetCode] 109. 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#109 Convert Sorted List to Binary Search Tree
原题地址 跟Convert Sorted Array to Binary Search Tree(参见这篇文章)类似,只不过用list就不能随机访问了. 代码: TreeNode *buildBST( ...
- leetcode -day19 Convert Sorted List to Binary Search Tree
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted ...
- 37. leetcode 108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)
108. Convert Sorted Array to Binary Search Tree 描述 Given an array where elements are sorted in ascen ...
随机推荐
- Installation failed with message Failed to finalize session : INSTALL_FAILED_INVALID_APK: Split lib_slice_8_apk was defined multiple times. It is possible that this issue is resolved by uninstalling a
取消:Instant Run就行
- 「小程序JAVA实战」Springboot版mybatis逆向生成工具(32)
转自:https://idig8.com/2018/08/29/xiaochengxujavashizhanspringbootbanmybatisnixiangshengchenggongju32/ ...
- then()方法是异步执行
then()方法是异步执行 就是当.then()前的方法执行完后再执行then()内部的程序 这样就避免了,数据没获取到等的问题
- JDBC中,如何动态的设置查询条件
今天看JDBC,发现有段代码,可以减少重复的编写查询方法,如下: public List<Goddess> query(List<Map<String, Object>& ...
- Proxmox 安装 dsm 黑群 备忘
备忘:Proxmox 虚拟机使用 E1000网卡(用Virlo找不到引导),直通数据硬盘 . 使用的引导文件是 DS918+_6.21-23824-1.04b.img 虚拟机启动使用第三项EX ...
- 将DataTable中的数据导出成Excel
public bool ExportFile(System.Data.DataTable dt){ SaveFileDialog sfd = new SaveFileDialog(); s ...
- 一道面试题Lintcode196-Find the Missing Number
http://www.lintcode.com/en/problem/find-the-missing-number/# Find the Missing Number Given an array ...
- 快速上手Runtime(二)之给分类添加属性
我们都知道,分类是不能直接添加属性的,那么我们有时候又需要实现这个功能,那么我们应该怎么办才能为分类添加上属性呢. Runtime给分类添加属性原理 给一个类声明属性,其实本质就是给这个类添加关联,并 ...
- SQL 数据库 学习 003 什么是数据库? 为什么需要数据库?是不是所有的软件都是用Sql Server?
什么是数据库? 为什么需要数据库? 是不是所有的软件都是用Sql Server? 我的电脑系统: Windows 10 64位 使用的SQL Server软件: SQL Server 2014 Exp ...
- 无需写try/catch,也能正常处理异常
对于企业应用的开发者来说,异常处理是一件既简单又复杂的事情.说其简单,是因为相关的编程无外乎try/catch/finally+throw而已:说其复杂,是因为我们往往很难按照我们真正需要的策略来处理 ...