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的更多相关文章

  1. [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 ...

  2. 【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 ...

  3. 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 ...

  4. [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. ...

  5. LeetCode Find Minimum in Rotated Sorted Array

    原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...

  6. 【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 ...

  7. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  8. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  9. 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 ...

  10. 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 ...

随机推荐

  1. Mac 电脑系统的重装

    首先来说一下我为什么会想到重装Mac的系统呢??? 其实呢  很简单的一个理由,在我写上一个项目的时候,在功能code编写完成后,在模拟器上运行是完全没有问题的,但是在真机上就不行,大家也都知道,在X ...

  2. Sql server2014 内存优化表 本地编译存储过程

    参考文献:http://www.infoq.com/cn/news/2013/09/Compiled-Queries http://www.bianceng.cn/database/SQLServer ...

  3. webview 设置编码

    WebSettings settings = webView.getSettings(); // 设置页面编码 settings.setDefaultTextEncodingName("ut ...

  4. C# WINFORM ListView用法详解(转)

    源代码下载位置: http://pan.baidu.com/s/1qXrLehe 一.ListView类 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false ...

  5. HttpSesstionActivationLIstener示例

    HttpSesstionActivationLIstener示例: http://www.cnblogs.com/xdp-gacl/p/3969249.html 钝化的session会已session ...

  6. 关于MVC结构

    简单的记录,只是想记录一下现在对MVC的理解. MVC,即模型(MODEL),视图(VIEW),控制器(CONTROLLER) 模型是数据模型 视图是图形界面 控制器是在两个之间的控制部分,用来将数据 ...

  7. IE和FF的差异

    ie和ff的差异   1. document.form.item 问题 1)现有问题: 现有代码中存在许多 document.formName.item("itemName") 这 ...

  8. ubuntu linux 设置环境变量

    添加环境变量 1.添加临时变量 终端中输入: PATH="$PATH:yourpath" :yourpath是要添加的环境变量(即要添加目录的绝对路径,例:/home/myprog ...

  9. arcpy.mapping实战-专题图制图自动化

    arcpy.mapping实战-专题图制图自动化 by 李远祥 最初对arcpy.mapping感兴趣是因为一次大规模的专题地图调整的需要,由于某某单位利用ArcEngine编写的专题图出图系统,出现 ...

  10. EntityFramewok Core 1.1连接MSSql数据库详解

    最近在研究ASP.NET Core,其中就用到了Entity Framework Core,对于Entity Framework Core连接SqlServer数据库,使用Code Frist创建数据 ...