当前位置:网站首页>LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
2022-06-26 05:13:00 【一瓢江湖我沉浮】
1.题目
给你一个整数数组 nums ,其中元素已经按升序排列,请你将其转换为一棵高度平衡二叉搜索树。
高度平衡二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。
示例 1:
输入:nums = [-10,-3,0,5,9]
输出:[0,-3,9,-10,null,5]
解释:[0,-10,5,null,-3,null,9] 也将被视为正确答案:
示例 2:
输入:nums = [1,3]
输出:[3,1]
解释:[1,null,3] 和 [3,1] 都是高度平衡二叉搜索树。
提示:
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums 按严格递增顺序排列
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree
2.思路
(1)递归构造
二叉树的构造过程大致为先:构造根节点,然后递归构建左右子树。而一个有序数组对于二叉搜索树来说就是其中序遍历结果,根节点在数组中心,数组左侧是左子树元素,右侧是右子树元素。
3.代码实现(Java)
//思路1————递归构造
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public TreeNode sortedArrayToBST(int[] nums) {
return build(nums, 0, nums.length - 1);
}
//将 nums[left...right] 转换为一棵高度平衡二叉搜索树
public TreeNode build(int[] nums, int left, int right) {
if (left > right) {
//nums[left...right]为空
return null;
}
//构造根节点
int mid = (left + right) / 2;
TreeNode root = new TreeNode(nums[mid]);
//递归构造左子树
root.left = build(nums, left, mid - 1);
//递归构造右子树
root.right = build(nums, mid + 1, right);
return root;
}
}
边栏推荐
- Mongodb image configuration method
- PHP one sentence Trojan horse
- date_ Range creation date range freq parameter value table and creation example
- 第九章 设置结构化日志记录(一)
- Fedora alicloud source
- Pytorch forecast house price
- Experience of reading the road to wealth and freedom
- 5. < tag stack and general problems > supplement: lt.946 Verify the stack sequence (the same as the push in and pop-up sequence of offer 31. stack)
- Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!
- The wechat team disclosed that the wechat interface is stuck with a super bug "15..." The context of
猜你喜欢

Baidu API map is not displayed in the middle, but in the upper left corner. What's the matter? Resolved!

LeetCode 19. 删除链表的倒数第 N 个结点

Tensorflow and deep learning day 3

2.< tag-动态规划和常规问题>lt.343. 整数拆分

Serious hazard warning! Log4j execution vulnerability is exposed!

apktool 工具使用文档

【上采样方式-OpenCV插值】

6.1 - 6.2 introduction to public key cryptography

Implementation of IM message delivery guarantee mechanism (II): ensure reliable delivery of offline messages

The beautiful scenery is natural, and the wonderful pen is obtained by chance -- how is the "wonderful pen" refined?
随机推荐
Machine learning final exercises
第九章 设置结构化日志记录(一)
Introduction to classification data cotegory and properties and methods of common APIs
cartographer_backend_constraint
程序人生
Yunqi lab recommends experience scenarios this week, free cloud learning
One of token passing between microservices @feign's token passing
两步处理字符串正则匹配得到JSON列表
Using requests library and re library to crawl web pages
Sentimentin tensorflow_ analysis_ layer
zencart新建的URL怎么重写伪静态
Official image acceleration
Technical past: tcp/ip protocol that has changed the world (precious pictures, caution for mobile phones)
Cookie and session Basics
ECCV 2020 double champion team, take you to conquer target detection on the 7th
The best Chinese open source class of vision transformer, ten hours of on-site coding to play with the popular model of Vit!
Final review of brain and cognitive science
Replacing domestic image sources in openwrt for soft routing (take Alibaba cloud as an example)
[quartz] read configuration from database to realize dynamic timing task
Sentimentin tensorflow_ analysis_ cell