当前位置:网站首页>生成二叉搜索平衡树[利用树递归特性]
生成二叉搜索平衡树[利用树递归特性]
2022-06-23 15:13:00 【REN_林森】
前言
对于树而言,就是一个递归结构,也致于处理好任意一颗子树,就处理好了整个树。所以递归结构就只有一个递归体这么简洁。利用树的递归特性,采用从下到上的子树生成+拼接来完成二叉搜索平衡树的生成。
一、有序链表转换二叉搜索树

二、从下到上回溯生成二叉搜索平衡树
package everyday.medium;
import java.util.ArrayList;
import java.util.List;
// 有序链表转换为二叉搜索树
public class SortedListToBST {
/* target:将升序单向链表转化成高度平衡二叉搜索树。 先把链表元素放数组中,便于递归取值。 树还是需要递归生成,而且需要从上往下生成。 */
public TreeNode sortedListToBST(ListNode head) {
// 取单向链表元素。
List<Integer> nums = new ArrayList<>();
while (head != null) {
nums.add(head.val);
head = head.next;
}
// 递归生成树。
return buildTree(nums, 0, nums.size() - 1);
}
private TreeNode buildTree(List<Integer> nums, int begin, int end) {
// 当begin > end时,说明没有可生成节点,那就生成null节点。
if (begin > end) return null;
// 当只有一个元素时,返回叶子节点。
if (begin == end) return new TreeNode(nums.get(begin));
// 递归生成树。
// 加1纯粹是看leetcode例子,当两个值时,选左 + 根。
// 可以不加1,则表示,当两个值时,选根 + 右。
int mid = begin + (end - begin + 1 >>> 1);
TreeNode root = new TreeNode(nums.get(mid));
root.left = buildTree(nums, begin, mid - 1);
root.right = buildTree(nums, mid + 1, end);
// 返回生成好的子树。
return root;
}
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
// 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;
}
}
}
总结
1)利用好树的递归特质。
2)从下到上生成并拼装树。
参考文献
边栏推荐
- Important knowledge of golang: atomic atomic operation
- Explore the "store" on the cloud. The cloud store is newly upgraded!
- Important knowledge of golang: detailed explanation of context
- 电荷泵原理讲义,电压是怎么“泵”上去的?
- spdlog记录日志示例 - 使用sink创建logger
- 电子学会图形化一级编程题解析:猫捉老鼠
- 他山之石 | 微信搜一搜中的智能问答技术
- Converging ecology, enabling safe operation, Huawei cloud security, cloud brain intelligent service security
- VGG下载(.net文件和imagenet-vgg-verydeep-19)
- Matlab| sparse auxiliary signal denoising and pattern recognition in time series data
猜你喜欢

JS里的数组

Redis集群操作的方法
![[普通物理] 光的衍射](/img/1a/20dbd15e0c8c91a3e59753b2f6797a.png)
[普通物理] 光的衍射

golang 重要知识:context 详解

30. concatenate substrings of all words

stylegan1: a style-based henerator architecture for gemerative adversarial networks

stylegan3:alias-free generative adversarial networks

英特尔Arc A380显卡消息汇总:跑分亮眼驱动拉胯 入门性价产品亟待优化

5 minutes to quickly launch web applications and APIs (vercel)

Arrays in JS
随机推荐
C. Product 1 Modulo N-Codeforces Round #716 (Div. 2)
JS garbage collection
Leetcode 450.删除二叉搜索树中的结点
Sfod: passive domain adaptation and upgrade optimization, making the detection model easier to adapt to new data (attached with paper Download)
CAS操作在ARM和x86下的不同实现
Variable declaration of go language
golang 重要知识:waitgroup 解析
Introduction to the push function in JS
32. Compose 优美的触摸动画
Une compréhension simple du tri rapide
Mysql database - log management, backup and recovery
Important knowledge of golang: rwmutex read / write lock analysis
英特尔Arc A380显卡消息汇总:跑分亮眼驱动拉胯 入门性价产品亟待优化
Wechat applet guides users to add applet animation page
看,这就是调制解调原理分析!附仿真文件
Which platform is a good place to open a futures account? Is it safe to open an online futures account?
FPGA 常用缩写及单词在工程领域内的意义
Thymeleaf——学习笔记
快速排序的簡單理解
Matlab| sparse auxiliary signal denoising and pattern recognition in time series data