当前位置:网站首页>给定二叉搜索树和两个整数A,B (最小整数和最大整数)。如何删除不在该区间内的元素(剪枝)
给定二叉搜索树和两个整数A,B (最小整数和最大整数)。如何删除不在该区间内的元素(剪枝)
2022-08-08 21:22:00 【lhf2112】
由于需要检查树中的每一个元素,结点的处理顺序可以是从叶子结点到根结点。这样当处理到结点本身时,其左子树和右字树为有效剪枝的BST。
static BianrySearchTreeNode PruneBST(BianrySearchTreeNode root, int A, int B){
if(root == null)
return null;
root.setLeft(PruneBST(root.getLeft(), A, B));
root.setRight(PruneBST(root.getRight(), A, B));
if(root.getData()>=A && root.getData()<=B)
return root;
if(root.getData()<A)
return root.getRight();
if(root.getData()>B)
return root.getLeft();
return root;
}
边栏推荐
猜你喜欢
随机推荐
C语言求积分的近似值
世界经济和金融秩序再定义 | 零数科技受邀出席第三届世界金融论坛
小程序-按钮透明无边框
用js写一个简单的前世今生
快速实现分列转到行(SQL版)一个问题,三种解法!
如何配合代理使用cURL?
分布式文件存储——分块上传和断点续传
小程序——切割字符串
Move your office environment anywhere with a solid state USB drive
Sentinel如何实现支持全局接口限流功能
ZERO Technology "Chain on the South"——deeply cultivated in the field of digital finance
爬虫系列:存储媒体文件
cURL是什么?
Zero Digital Reports Digital Financial Innovation to the Secretary of Hainan Provincial Party Committee
Contextual Transformer Networks for Visual Recognition paper and code analysis
IPv6 私有地址
文件上传接入阿里云OSS
中国石油大学(北京)-《 油田化学》第一阶段在线作业
oracle数据库的数据备份导出与数据导入恢复
上下分页功能实现