【数组递归构造二叉树】LeetCode 108. Convert Sorted Array to Binary Search Tree

C++语言程序设计教程第4版杨进才沈显君课后习题答案解析 为了更好地学习这门课程,我尝试在网上寻找一些学习资料和参考书籍,但效果并不是很理想。因此,我希望能有一些学长学姐或者是有经验的程序员能够分享一些学习经验和方法,让我更好地掌握这门课程。学习过程中,我发现这本书的内容非常丰富,讲解也非常详细,对于像我这样编程基础薄弱的学生来说,是一本非常实用的教材。同时,我也希望有经验的程序员能够推荐一些适合初学者的C++语言学习资料和工具,以便我能更好地学习和掌握这门课程。总之,希望能得到大家的帮助和支持,让我更好地学习和掌握C++语言程序设计。主 编: 杨进才,沈显君。 阅读详情

LeetCode 108. Convert Sorted Array to Binary Search Tree

Solution1:我的答案
构造二叉树利用递归

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* sortedArrayToBST(vector<int>& nums) {
        return my_sort(nums);
    }

    TreeNode* my_sort(vector<int> nums) {
        if (nums.size() == 0) return NULL;
        else if (nums.size() == 1) {
            TreeNode* root = new TreeNode(nums[0]);
            return root;
        }
        else {
            int root_index = nums.size()/2;
            TreeNode* root = new TreeNode(nums[root_index]);
            vector<int> left_tree, right_tree;
            for (int i = 0; i < nums.size(); i++) {
                if (i < root_index)
                    left_tree.push_back(nums[i]);
                else if (i > root_index)
                    right_tree.push_back(nums[i]);
            }
            root->left = my_sort(left_tree);
            root->right = my_sort(right_tree);
            return root;
        }
    }
};

Solution2:
参考网址:http://www.cnblogs.com/grandyang/p/4295245.html
下面是避免数组元素大量复制的写法,还是这种好 啊

class Solution {
public:
    TreeNode *sortedArrayToBST(vector<int> &num) {
        return sortedArrayToBST(num, 0 , num.size() - 1);
    }
    TreeNode *sortedArrayToBST(vector<int> &num, int left, int right) {
        if (left > right) return NULL;
        int mid = (left + right) / 2;
        TreeNode *cur = new TreeNode(num[mid]);
        cur->left = sortedArrayToBST(num, left, mid - 1);
        cur->right = sortedArrayToBST(num, mid + 1, right);
        return cur;
    }
};
【慧创近红外脑功能成像(fNIRS)】经皮耳迷走神经电刺激联用应用集锦 在这项双盲、随机、对照的试验中,将40例亚急性脑卒中患者随机分为两组:VNS组(VG)在TOT期间接受taVNS治疗,假手术组(SG)在TOT期间接受假taVNS治疗。此外,fNIRS显示,与SG相比,VG的对侧前额叶皮层和同侧感觉运动皮层的激活增加。然而,HRV组间无显著差异。结合taVNS技术,不仅能够探索taVNS对大脑功能产生的具体影响,还能通过精细调整刺激参数,进一步优化治疗效果,使之更加精准高效。这种治疗模式的创新,有望显著提升患者的治疗效果,改善其生活质量,为神经系统疾病的康复开辟新途径。 阅读详情

相关推荐

不用加速器!Google Earth Pro登录失败自救手册:从服务器连接到地图加载的完整排错流程

本文详细解析了Google Earth Pro登录失败的常见问题及解决方案,包括服务器连接失败、地图加载异常等。通过基础排查、内置修复工具使用和高级配置调整,帮助用户无需加速器即可解决启动问题、黑屏和转圈圈等困扰,提升软件使用体验。

weixin_29227425的博客 486

LeetCode ACM模式——二叉树篇(一)

二叉树的定义、创建以及前、中、后序遍历

zssxcj的博客 1452

信噪比(Signal-to-Noise Ratio, SNR)详细介绍

信噪比(Signal-to-Noise Ratio,SNR)是衡量信号质量的重要参数,表示有用信号的功率与背景噪声功率的比值。其中,\( P_{\text{signal}} \) 表示信号功率,\( P_{\text{noise}} \) 表示噪声功率。- 音频处理:在音频设备和录音系统中,SNR决定了音质的好坏,高SNR表示音频信号更清晰,噪声更少。- 低SNR:信号中噪声成分较大,接收端难以分辨有用信号和噪声,可能导致误码率高,通信质量差。SNR定义为信号功率与噪声功率之比,通常用分贝(dB)表示。

qq_58860480的博客 8万+

从数组形式创建一棵树(用于leetcode测试)

这段时间时不时地会在leetcode上做些题,最近做到的大部分是与树相关的题。由于是在本地的IDE上码代码,如果每次测试都要...

2924

高熵合金AlCoCrFeNi/金刚石摩擦案例代码

本文介绍金刚石与高熵合金AlCoCrFeNi摩擦的案例。 在之前的文章中,介绍过使用fix indent命令实现虚拟球的摩擦,本文介绍使用金刚石实体球实现高熵合金摩擦的方法。 主要的模拟过程如下: (1)基体建模 建使用原子替换法获得高熵合金初始模型,升温到3000K驰豫后,冷却至300K,获得平衡组织。 (2)摩擦球建模 在高熵合金平衡组织的模型中添加金刚石摩擦球,使用change_box命令扩大box尺寸,添加金刚石摩擦球,并设置混合力场。 (3)摩擦过程 摩擦球下压一定深度后,沿y方向进行摩擦,摩擦

lammps_jiayou的博客 2900

uniapp - 解决报错 TypeError: e[t] is not a function(Uniapp项目运行或打包编译时提示错误e[t] is not a function,全平台端均可搞定)

uniapp,报错,TypeError: e[t] is not a function,uniapp App端报错,Android端,苹果ios打包报错,运行报错,uniapp打包报错e[t] is not a function,无法运行提示错误,uniapp live-pusher直播推流,maps地图组件,uni-app live-pusher 报错TypeError: e[t] is not a function,uni-app maps报错TypeError: e[t] is not a funct

✅ 精选推荐文章 8223

LeetCode刷题助手:根据数组生成二叉树

大家在刷LeetCode的二叉树相关的题目的时候,遇到问题需要在本地IDE里调试的时候,题目里给的二叉树的形式是一个数组,类似 Integer[] array = new Integer[]{1, null, 2, null, null, null, 3}; 这种形式,它是以null来做填充,构成一个完全二叉树,具体的图我就不画了,大家自己理解一下。而我们需要自己手动把它转换成二叉树的形式,...

dbqb007的专栏 8229

JAX/JAXlib GPU 加速版本安装教程(CUDA)

本文介绍了在Windows WSL2+Ubuntu22.04环境下配置JAX/JAXlib GPU加速版本的详细步骤。使用RTX 4060显卡,安装CUDA 12.4和cuDNN 8.9.7驱动,通过Miniconda创建Python 3.9虚拟环境。具体步骤包括:安装Miniconda、配置CUDA环境变量、安装cuDNN驱动、下载并安装JAX/JAXlib。最后通过测试代码验证GPU加速功能,成功检测到GPU并完成矩阵运算。整个过程配有详细命令和截图说明,适合需要搭建JAX GPU开发环境的用户参考。

猿小白的博客 629

LeetCode,构造二叉树

LeetCode,构造二叉树

qq_45031079的博客 779

【电源】电机一启动或负载电流瞬间增大,电源输出电压就下降,欠压,随后又恢复正常

系列文章目录 1.元件基础 2.电路设计 3.PCB设计 4.元件焊接 5.板子调试 6.程序设计 7.算法学习 8.编写exe 9.检测标准 10.项目举例 11.职业规划 亲身经历,特记录下来,分享经验 一、问题描述 用的时创联B-150-24开关电源,这个电源可以同时输出24V 5.8A和5V 2A。5V输出端接的是一个固定1.3A的负载。24V端接的是0.7A的固定负载和一个直流电机。开机后,开启直流电机瞬间,此时直流电机上流过的电流最大是2.5A,24V端口处电流测得0.9A,电源盒

云轩阁 6791

微信小程序一键跳转公众号(使用 official-account 组件)

在当今移动互联网时代,用户体验已成为应用成功的关键因素之一。微信小程序作为一种便捷的应用形式,广泛应用于各个行业,为用户提供了丰富的服务和信息。然而,如何进一步提升用户体验,使其在小程序中能够更方便地获取相关信息,成为开发者需要关注的重要课题。实现一键跳转到公众号的功能,正是一个有效的解决方案。通过这一功能,用户可以轻松访问公众号,获取更多内容和服务,从而增强与品牌的互动和粘性。本文将详细介绍如何在微信小程序中实现这一功能,并提供相关的操作步骤和注意事项。

不迁怒,不贰过。小知识,大智慧。 3128

LeetCode用数组建立二叉树

LeetCode利用数组构建二叉树

游吟焰火的博客 1543

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

LeetCode 108. Convert Sorted Array to Binary Search Tree 题解

本题是二叉搜索树的基础问题,主要考察递归构建树的技巧。选择中间元素作为根节点,保证平衡性递归构建左右子树注意整数溢出问题通过本题可以深入理解二叉搜索树的性质和递归构建树的方法。

cannonjinx的博客 4063

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

leetcode108. Convert Sorted Array to Binary Search Tree

题目描述 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of...

中华小当家的博客 359

LeetCode-108. Convert Sorted Array to Binary Search Tree

https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Solution # Definition f...

a1669380588的博客 126

LeetCode108 —— Convert Sorted Array to Binary Search Tree

这道题的意思是给你一个排好序的数组,如何建立一棵平衡二叉查找树。一开始还是不知道怎么去搞的,但是想了想这是道简单题,那应该不会很复杂,因为数组是排好序的,所以肯定有什么小trick。 于是我就直接打开了讨论区去看(真的想不到什么简单的方法去做),就发现做法还是蛮简洁的,既然是排序后的数组,且左子树和右子树相差不超过1,那么root肯定就是数组的中位数,然后再递归一下就好了。代码如下: class ...

qq_30650047的博客 329

LeetCode题解:Convert Sorted Array to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 思路: 给定一个区间[left, right],取其中值mid=(left+right)/2对应的元素作为二

Magi Su的技术回收站 4534

leetcode 108. Convert Sorted Array to Binary Search Tree-数组转搜索树|递归|非递归

原题链接:108. Convert Sorted Array to Binary Search Tree 【思路-Java、Python】-递归实现 首先要明白二叉搜索树的性质——任意一个节点的值都不小于它的左子树上任一节点的值,任意一个节点的值不大于它的右子树上任一节点的值。高度平衡的二叉搜索数,要求平衡因子不大于1。 题目给定一个已经排好序的数组,要求构造一个高度平衡的二叉搜索树。那么,

hdtopku 1621

LeetCode108——Convert Sorted Array to Binary Search Tree

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which th...

dingze7757的博客 244

leetcode 108. Convert Sorted Array to Binary Search Tree 详解 python3

一.问题描述 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the dept...

CSerwangjun的博客 387

LeetCode Top Interview Questions 108. Convert Sorted Array to Binary Search Tree (Java版; Easy)

welcome to my blog LeetCode Top Interview Questions 108. Convert Sorted Array to Binary Search Tree (Java版; Easy) 题目描述 Given an array where elements are sorted in ascending order, convert it to a heig...

littlehaes的博客 521

LeetCode 108. Convert Sorted Array to Binary Search Tree

LeetCode 108. Convert Sorted Array to Binary Search Tree(Easy) 主要知识点:树、DFS;优先级:4

温迪今天刷题了吗 137

leetcode python3 简单题108. Convert Sorted Array to Binary Search Tree

1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零八题 (1)题目 英文: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-...

从此做一个俗人 340

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. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth...

fanyuwgy的博客 146

[LeetCode] 108. Convert Sorted Array to Binary Search Tree

原题链接: 1. 题目介绍 2. 解题思路

Ber03的博客 207

leetCode 108. Convert Sorted Array to Binary Search Tree JAVA

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.    这到题把有序数组转成平衡二叉搜索树,所以选取数组的中点num[mid]作为根,对左右两边分

记忆力不好的博客 1390

LeetCode C++ 108. Convert Sorted Array to Binary Search Tree【Tree】简单

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1 .

memcpy0的博客 379
上一篇: LeetCode 98. Validate Binary Search Tree
下一篇: 【链表递归构造二叉树】LeetCode 109. Convert Sorted List to Binary Search Tree
Allenlzcoder
博客等级 码龄10年 925粉丝 512原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值