[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 ...
随机推荐
- python学习——练习题(8)
""" 题目:输出 9*9 乘法口诀表. """ def answer1(): """ 自己用最普通的双重循环 ...
- 【译】PGS字幕
PGS(Presentation graphic stream):图形字幕流,是用来显示蓝光电影中的字幕的流.当蓝光盘中的PGS格式的字幕被分离存储的时候通常保存在一个以sup为扩展名的文件中.(也可 ...
- VS2013 查看程序各个函数的CPU利用率<转>
自己写的程序CPU占用率过高,无法锁定原因时,可以用VS2013帮忙检测 1. 打开VS 性能分析 2. 启动项目进行检测 3. 选择CPU采样 完成 4. 分析一段时间 然后停止分析 5. 选择显示 ...
- effective javascript 学习心得
第2条:理解JavaScript的浮点数 1.js数字只有一种类型 2.见代码 /** * Created by Administrator on 2017/7/2. */ console.log(& ...
- LUA使用虚函数与使用回调函数
------------------虚函数overload-------------------------- --回调:寻路中格子坐标改变 CHero.OnSearchToCellsChange = ...
- 【300】◀▶ IDL - ENVI API
参考:ENVI API 参考:ENVI Classic Display 序号 类名称 功能说明 语法 & 举例 01 ENVI 函数 ====<<<< De ...
- 常用sql语句备份
1.查看数据库中所有的存储过程 use [DataBase_Name] go SELECT * FROM sys.all_objects WHERE ([type] = 'P' OR [type] = ...
- 迷你MVVM框架 avalonjs 组件编写指南
avalon经过半年的宣传,已经有不少公司在使用avalon应用于它们内外网应用或移动项目,比较大牌的客户有百度,搜狐,金山,边缘,去哪儿--最近成为去哪儿的前端架构师后,掌握更多资源,可以随使抓个人 ...
- Ubuntu 安装 kamailio
首先安装前,你已经对kamailio的基本用法了解.可根据情况选择安装方式,本次安装基于Ubuntu18.04系统安装,对于16.04及一下会遇到版本问题,请自己查阅文档解决 安装第三方库 sudo ...
- react native android 真机调试
http://localhost:8081/index.android.bundle?platform=android 晕死,设备掉线了 C:\Users\ZHONGZHENHUA\.android\ ...