当前位置:网站首页>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;
}
};
边栏推荐
- pathman_config、pathman_config_params 删除后,如何重建?
- 轮转数组问题:如何实现数组“整体逆序,内部有序”?“三步转换法”妙转数组
- How does MSP430 download programs to the board?(IAR MSPFET CCS)
- 基于改进YOLOv5轻量化的烟火检测
- font
- 21 Day Learning Challenge Week 1 Summary
- 二叉树相关代码题【较全】C语言
- Idea (优选)cherry-pick操作
- 树莓派入门(5)系统备份
- The 125th day of starting a business - a note
猜你喜欢
添加用户报错useradd: cannot open /etc/passwd
云平台下ESB产品开发步骤说明
[yu gong series] Go program 035-08 2022 interfaces and inheritance and transformation and empty interface
【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion
The problem that Merge will be lost again after code Revert has been solved
互换性与测量技术-公差原则与选用方法
A simple JVM tuning, learn to write it on your resume
Salesforce disbands the Chinese team, which CRM product is more suitable for the Chinese
Is Redis old?Performance comparison between Redis and Dragonfly
字体反扒
随机推荐
"Life Is Like First Seen" is ill-fated, full of characters, and the contrast of Zhu Yawen's characters is too surprising
Detailed explanation of VIT source code
【LeetCode】Day112-repetitive DNA sequence
字体反扒
轮转数组问题:如何实现数组“整体逆序,内部有序”?“三步转换法”妙转数组
oracle的基数会影响到查询速度吗?
What has programmatic trading changed?
Add user error useradd: cannot open /etc/passwd
电力机柜数据监测RTU
C语言之自定义类型------结构体
EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
UNI-APP_iphone苹果手机底部安全区域
二叉树相关代码题【较全】C语言
En-us is an invalid culture error solution when Docker links sqlserver
图解LeetCode——640. 求解方程(难度:中等)
浅析一下期货程序化交易好还是手工单好?
树莓派入门(5)系统备份
How to rebuild after pathman_config and pathman_config_params are deleted?
Goodbye Chongqing paper invoices!The issuance of electronic invoices for accommodation expenses will soon completely replace the invoices of hotels, catering and gas stations
A practice arrangement about map GIS (below) GIS practice of Redis