41.有序数组(二叉搜索树)转平衡二叉树

1.题目描述

2.解题思路

递归,依旧看basecase情况

cpp 复制代码
class Solution {
public:
    
    TreeNode* helper(vector<int>& nums, int l, int r){
        if(l > r){
            return nullptr;
        }
   
    int mid = (l + r)/2;
    TreeNode *root  = new TreeNode(nums[mid]);
    root->left = helper(nums, l , mid - 1);
    root->right = helper(nums, mid + 1, r);
    return root;
}
    
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        return helper(nums, 0, nums.size() - 1);
        
    }
};
相关推荐
m0_547486665 天前
《数据结构教程》全套 PPT课件2026
数据结构
tryxr6 天前
矩阵的几种基础变换
java·数据结构·算法·矩阵
mmmmath_36 天前
LeetCode.028.找出字符串中第一个匹配项的
数据结构·算法·leetcode
All for pursuit.6 天前
【栈-4】739.每日温度
数据结构·c++·算法·leetcode
All for pursuit.6 天前
【栈-5】84.柱状图中最大的矩形
数据结构·c++·算法·leetcode
渡我白衣6 天前
HttpRequest与HttpResponse的实现
服务器·数据结构·c++·人工智能·tcp/ip·机器学习·caffe
晴天的雨.9926 天前
【C++算法】和为s的两个数
开发语言·数据结构·c++·算法
淡海水6 天前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
Logic1016 天前
C语言/数据结构位运算题解:异或XOR找出时尚聚会中的“独特颜色“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
2401_839080546 天前
C++常见八股
数据结构·c++·算法