当前位置:网站首页>Leetcode 669. 修剪二叉搜索树
Leetcode 669. 修剪二叉搜索树
2022-08-11 03:29:00 【LuZhouShiLi】
Leetcode 669. 修剪二叉搜索树
题目
给你二叉搜索树的根节点 root ,同时给定最小边界low 和最大边界 high。通过修剪二叉搜索树,使得所有节点的值在[low, high]中。修剪树 不应该 改变保留在树中的元素的相对结构 (即,如果没有被移除,原有的父代子代关系都应当保留)。 可以证明,存在 唯一的答案 。
所以结果应当返回修剪好的二叉搜索树的新的根节点。注意,根节点可能会根据给定的边界发生改变
思路
- 确定递归函数的参数和返回值:通过递归函数的返回值来删除节点
- 确定终止条件:遇到空结点返回即可
- 确定单层递归逻辑:
- 如果root元素值小于low,那么递归右子树,并且返回右子树中符合条件的节点
- 如果root元素值大于high,那么递归左子树,并且返回左子树中符合条件的节点
- 接下来将下一层递归处理左子树的结果赋值给root->left,处理右子树的结果赋值给root->right,最后返回root节点
代码
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int low, int high) {
if(root == nullptr) return nullptr;
if(root->val < low)
{
TreeNode* right = trimBST(root->right,low,high);
return right;
}
if(root->val > high)
{
TreeNode* left = trimBST(root->left,low,high);
return left;
}
root->left = trimBST(root->left,low,high);// root->left接入符合条件的左孩子
root->right = trimBST(root->right,low,high);// root->right接入符合条件的右孩子
return root;
}
};
边栏推荐
- 重庆纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
- 轮转数组问题:如何实现数组“整体逆序,内部有序”?“三步转换法”妙转数组
- What problems should we pay attention to when building a programmatic trading system?
- A practice arrangement about map GIS (below) GIS practice of Redis
- 【LeetCode】Day112-重复的DNA序列
- Homework 8.10 TFTP protocol download function
- uni-app - 城市选择索引列表 / 通过 A-Z 排序的城市列表(uview 组件库 IndexList 索引列表)
- this question in js
- 一次简单的 JVM 调优,学会拿去写到简历里
- 浅析一下期货程序化交易好还是手工单好?
猜你喜欢
树莓派入门(5)系统备份
Detailed explanation of VIT source code
CSDN blog replacement skin
按摩椅控制板的开发让按摩椅变得简约智能
【愚公系列】2022年08月 Go教学课程 036-类型断言
"How to kick a bad habit to read notes?
Build Zabbix Kubernetes cluster monitoring platform
AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
多商户商城系统功能拆解26讲-平台端分销设置
E-commerce project - mall time-limited seckill function system
随机推荐
Homework 8.10 TFTP protocol download function
大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?
Will oracle cardinality affect query speed?
阿里低代码框架 lowcode-engine 之自定义物料篇
Window function application of sum and count
Add user error useradd: cannot open /etc/passwd
构建程序化交易系统需要注意什么问题?
增加对 Textbundle 的支持
Official release丨VS Code 1.70
CSDN blog replacement skin
Idea (优选)cherry-pick操作
E-commerce project - mall time-limited seckill function system
The "top pillar" slides, and new growth is extremely difficult to shoulder the heavy responsibility. Is Ali "squatting" to jump higher?
二叉树相关代码题【较全】C语言
DNS separation resolution and intelligent resolution
没想到MySQL还会问这些...
Paper Accuracy - 2017 CVPR "High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis"
A brief analysis of whether programmatic futures trading or manual order is better?
Detailed explanation of VIT source code
How to delete statements audit log?