当前位置:网站首页>二叉搜索树(特性篇)
二叉搜索树(特性篇)
2022-07-07 14:17:00 【Joey Liao】
首先,BST 的特性大家应该都很熟悉了:
- 对于 BST 的每一个节点
node
,左子树节点的值都比node
的值要小,右子树节点的值都比node
的值大。 - 对于 BST 的每一个节点 node,它的左侧子树和右侧子树都是 BST。
二叉搜索树并不算复杂,但我觉得它可以算是数据结构领域的半壁江山,直接基于 BST 的数据结构有 AVL 树,红黑树等等,拥有了自平衡性质,可以提供 logN 级别的增删查改效率;还有 B+ 树,线段树等结构都是基于 BST 的思想来设计的。
从做算法题的角度来看 BST,除了它的定义,还有一个重要的性质:BST 的中序遍历结果是有序的(升序)。
也就是说,如果输入一棵 BST,以下代码可以将 BST 中每个节点的值升序打印出来:
void traverse(TreeNode root) {
if (root == null) return;
traverse(root.left);
// 中序遍历代码位置
print(root.val);
traverse(root.right);
}
寻找第 K 小的元素
力扣第 230 题「 二叉搜索树中第 K 小的元素」
,一个直接的思路就是升序排序,然后找第 k 个元素呗。BST 的中序遍历其实就是升序排序的结果,找第 k 个元素肯定不是什么难事。
int kthSmallest(TreeNode root, int k) {
// 利用 BST 的中序遍历特性
traverse(root, k);
return res;
}
// 记录结果
int res = 0;
// 记录当前元素的排名
int rank = 0;
void traverse(TreeNode root, int k) {
if (root == null) {
return;
}
traverse(root.left, k);
/* 中序遍历代码位置 */
rank++;
if (k == rank) {
// 找到第 k 小的元素
res = root.val;
return;
}
/*****************/
traverse(root.right, k);
}
如果按照我们刚才说的方法,利用「BST 中序遍历就是升序排序结果」这个性质,每次寻找第 k
小的元素都要中序遍历一次,最坏的时间复杂度是 O(N)
,N
是 BST 的节点个数。
要知道 BST 性质是非常牛逼的,像红黑树这种改良的自平衡 BST,增删查改都是 O(logN)
的复杂度,让你算一个第 k
小元素,时间复杂度竟然要 O(N)
,有点低效了。
那么回到这个问题,想找到第 k
小的元素,或者说找到排名为 k
的元素,如果想达到对数级复杂度,关键也在于每个节点得知道他自己排第几。
比如说你让我查找排名为 k
的元素,当前节点知道自己排名第 m
,那么我可以比较 m
和 k
的大小:
如果 m == k,显然就是找到了第 k 个元素,返回当前节点就行了。
如果 k < m,那说明排名第 k 的元素在左子树,所以可以去左子树搜索第 k 个元素。
如果 k > m,那说明排名第 k 的元素在右子树,所以可以去右子树搜索第 k - m - 1 个元素。
这样就可以将时间复杂度降到 O(logN) 了。
那么,如何让每一个节点知道自己的排名呢?
这就是我们之前说的,需要在二叉树节点中维护额外信息。每个节点需要记录,以自己为根的这棵二叉树有多少个节点。
也就是说,我们 TreeNode 中的字段应该如下:
class TreeNode {
int val;
// 以该节点为根的树的节点总数
int size;
TreeNode left;
TreeNode right;
}
有了 size
字段,外加 BST 节点左小右大的性质,对于每个节点 node
就可以通过 node.left
推导出 node
的排名,从而做到我们刚才说到的对数级算法。
当然,size
字段需要在增删元素的时候需要被正确维护,力扣提供的 TreeNode
是没有 size
这个字段的,所以我们这道题就只能利用 BST 中序遍历的特性实现了,但是我们上面说到的优化思路是 BST 的常见操作,还是有必要理解的。
BST 转化累加树
力扣第 538 题和 1038 题都是这道题 把二叉搜索树转换为累加树
其实就是二叉树的中序遍历,用sum
来记录当前累加和
/** * 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 {
int sum=0;
public TreeNode convertBST(TreeNode root) {
traverse(root);
return root;
}
public void traverse(TreeNode root){
if(root==null){
return ;
}
traverse(root.right);
sum+=root.val;
root.val=sum;
traverse(root.left);
}
}
边栏推荐
- [excelexport], Excel to Lua, JSON, XML development tool
- The difference and working principle between compiler and interpreter
- 目标跟踪常见训练数据集格式
- Set the route and optimize the URL in thinkphp3.2.3
- Regular expression string
- Three. JS series (2): API structure diagram-2
- 01tire+ chain forward star +dfs+ greedy exercise one
- Laravel 服务提供者实例教程 —— 创建 Service Provider 测试实例
- Bidding announcement: Fujian Rural Credit Union database audit system procurement project (re bidding)
- Bidding announcement: 2022 Yunnan Unicom gbase database maintenance public comparison and selection project (second) comparison and selection announcement
猜你喜欢
pycharm 终端部启用虚拟环境
Xcode Revoke certificate
Odoo集成Plausible埋码监控平台
The team of East China Normal University proposed the systematic molecular implementation of convolutional neural network with DNA regulation circuit
2022 the 4th China (Jinan) International Smart elderly care industry exhibition, Shandong old age Expo
1亿单身男女“在线相亲”,撑起130亿IPO
AutoLISP series (1): function function 1
Three. JS series (2): API structure diagram-2
模仿企业微信会议室选择
Pycharm terminal enables virtual environment
随机推荐
Tragedy caused by deleting the console statement
Unity drawing plug-in = = [support the update of the original atlas]
AutoLISP series (2): function function 2
[flower carving experience] 15 try to build the Arduino development environment of beetle esp32 C3
php 自带过滤和转义函数
Step by step monitoring platform ZABBIX
Opportunity interview experience summary
[summary of knowledge] summary of notes on using SVN in PHP
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
logback. XML configure logs of different levels and set color output
Imitate the choice of enterprise wechat conference room
What is the difference between IP address and physical address
markdown公式编辑教程
PHP realizes wechat applet face recognition and face brushing login function
Description of vs common shortcut keys
Eye of depth (VII) -- Elementary Transformation of matrix (attachment: explanation of some mathematical models)
Sysom case analysis: where is the missing memory| Dragon lizard Technology
Logback logging framework third-party jar package is available for free
laravel中将session由文件保存改为数据库保存
95. (cesium chapter) cesium dynamic monomer-3d building (building)