Convert Sorted Array to Binary Search Tree & Convert Sorted List to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
Subscribe to see which companies asked this question
要点就是找到中心点,然后分别递归构造左边的数和右边的数
TreeNode* sortedArrayToBST(vector<int>& nums, int beg, int end) {
if (beg > end)
return nullptr;
int mid = (beg + end) >> ;
TreeNode* root = new TreeNode(nums[mid]);
root->left = sortedArrayToBST(nums, beg, mid - );
root->right = sortedArrayToBST(nums, mid + , end);
return root;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
return sortedArrayToBST(nums, , nums.size()-);
}
链表为了代码的简洁,我使用了二级指针,不过可读性变差了
TreeNode* sortedListToBST(ListNode* head) {
if (head == nullptr)
return nullptr;
ListNode* fast = head;
ListNode** slow = &head;
while (fast->next != nullptr && fast->next->next != nullptr)
{
fast = fast->next->next;
slow = &((*slow)->next);
}
TreeNode* root = new TreeNode((*slow)->val);
root->right = sortedListToBST((*slow)->next);
*slow = nullptr;
root->left = sortedListToBST(head);
return root;
}
Convert Sorted Array to Binary Search Tree & Convert Sorted List to Binary Search Tree的更多相关文章
- [LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...
- [LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- LeetCode Find Minimum in Rotated Sorted Array
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...
- 【LeetCode】153. Find Minimum in Rotated Sorted Array (3 solutions)
Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 88. Merge Sorted Array【easy】
88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...
- 159. Find Minimum in Rotated Sorted Array 【medium】
159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...
- LeetCode--Array--Remove Duplicates from Sorted Array (Easy)
26. Remove Duplicates from Sorted Array (Easy) Given a sorted array nums, remove the duplicates in-p ...
随机推荐
- poj 1013(uva 608) Counterfeit Dollar
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #in ...
- Eclipse寻找JVM的机制
Eclipse寻找JVM的机制 查看当前用了哪个jvm的方法: Help->About Eclipse -> Installation Details ->Configuration ...
- MySQL各存储引擎
MySQL中的数据用各种不同的技术存储在文件(或者内存)中.这些技术中的每一种技术都使用不同的存储机制.索引技巧.锁定水平并且最终提供广泛的不同的功能和能力.通过选择不同的技术,你能够获得额外的速度或 ...
- JAVA finally字句的异常丢失和返回值覆盖解析
转载:http://blog.csdn.net/sureyonder/article/details/5560538 Java虚拟机在每个try语句块和与其相关的catch子句的结尾 处都会“调用”f ...
- jq基础
$(function() { $(".dd").attr("class","cc").append("<h ...
- 通过判断cookie过期方式向Memcached中添加,取出数据(Java)
应用场景:在数据驱动的web开发中,经常要重复从数据库中取出相同的数据,这种重复极大的增加了数据库负载.缓存是解决这个问题的好办法.但是ASP.NET中的虽然已经可以实现对页面局部进行缓存,但还是不够 ...
- 简单的Elf逆向Writeup
ElfCrackMe1 html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acrony ...
- [译] 关于 Git 你需要知道的一些事情
分支和合并 Git 跟其他版本控制系统最大的优势就在于其高级的分支模型. Git 允许而且 鼓励 你在本地使用多个完全独立的分支.这些分支的创建,合并和删除几乎都可以在几秒内完成. 这意味着你可以轻松 ...
- http协议详解(超详细)
http1. 基础概念篇 1.1 介绍 HTTP是Hyper Text Transfer Protocol(超文本传输协议)的缩写.它的发展是万维网协会(World Wide Web Consorti ...
- 众人拾柴火焰高之Tomcat集群
一人拾柴火不旺,众人拾柴火焰高.Tomcat服务器也是一样,一台服务器再强大能承受的访问也是有限的.要提供高并发.高可用的服务,就必须横向扩展,多台Tomcat组成一个集群,根据实际的访问量动态增减服 ...