当前位置: 首页 > news >正文

胶州做网站公司网站批量查询工具

胶州做网站公司,网站批量查询工具,最成功的个人网站,新疫情最新消息题目来源:https://leetcode.cn/problems/trim-a-binary-search-tree/description/ C题解1:递归法。当前节点为空时返回空,不为空时对其值进行分类讨论。以low为例,当前节点值等于low时,意味着其左子树都要丢弃&#xf…

题目来源:https://leetcode.cn/problems/trim-a-binary-search-tree/description/

 

 C++题解1:递归法。当前节点为空时返回空,不为空时对其值进行分类讨论。以low为例,当前节点值等于low时,意味着其左子树都要丢弃,可指向空;大于low时,说明其左子树也可能满足条件,因此对其左子树进一步递归;小于low时,说明当前节点及其左子树都不满足条件,将当前节点更新为其右子节点。

class Solution {
public:TreeNode* trimBST(TreeNode* root, int low, int high) {if(!root) return nullptr;if(root->val == low) root->left = nullptr; else if(root->val > low) {root->left = trimBST(root->left, low, high);}else {root = root->right;return trimBST(root, low, high);}if(root->val == high) root->right = nullptr;else if(root->val < high) {root->right = trimBST(root->right, low, high);}else {root = root->left;return trimBST(root, low, high);}return root;}
};

C++题解2:递归法。大致思路同上,较为精简,来源代码随想录。

class Solution {
public:TreeNode* trimBST(TreeNode* root, int low, int high) {if (root == nullptr) return nullptr;if (root->val < low) return trimBST(root->right, low, high);if (root->val > high) return trimBST(root->left, low, high);root->left = trimBST(root->left, low, high);root->right = trimBST(root->right, low, high);return root;}
};

C++题解3:迭代法。见注释,来源代码随想录。

class Solution {
public:TreeNode* trimBST(TreeNode* root, int L, int R) {if (!root) return nullptr;// 处理头结点,让root移动到[L, R] 范围内,注意是左闭右闭while (root != nullptr && (root->val < L || root->val > R)) {if (root->val < L) root = root->right; // 小于L往右走else root = root->left; // 大于R往左走}TreeNode *cur = root;// 此时root已经在[L, R] 范围内,处理左孩子元素小于L的情况while (cur != nullptr) {while (cur->left && cur->left->val < L) {cur->left = cur->left->right;}cur = cur->left;}cur = root;// 此时root已经在[L, R] 范围内,处理右孩子大于R的情况while (cur != nullptr) {while (cur->right && cur->right->val > R) {cur->right = cur->right->left;}cur = cur->right;}return root;}
};

http://www.hkea.cn/news/742192/

相关文章:

  • 临沂网站建设那家好小米市场营销案例分析
  • 德化网站建设企业中层管理人员培训课程
  • 网站怎么通过流量赚钱爱站网能不能挖掘关键词
  • 网站建设课后感营销型网站有哪些平台
  • 哪个网站做生鲜配送厦门seo外包公司
  • 水电行业公司设计logo重庆seo排名扣费
  • 可信赖的南昌网站制作站长工具网站
  • 济南建站公司电话成都关键词自然排名
  • 门户网站开发公司推广网页
  • 如何做网站认证实时军事热点
  • 上海的网站建设公司哪家好企业网站建设
  • 专业b2c电商网站制作网站推广要点
  • 现在的网站用什么程序做百度云官网登录入口
  • vs做网站怎样加数据库新闻小学生摘抄
  • 广州做网站mxszpt小说排行榜
  • 有什么网站是python做的网站营销策划公司
  • 长春有什么好的网站制作公司链接购买
  • 毕设网站佛山网站建设十年乐云seo
  • 北京做网站建设的公司哪家好手机怎么创建网站
  • winforms做网站注册百度账号
  • 玉泉路网站建设营销培训课程有哪些
  • 渭南做网站费用搜索引擎排名优化是什么意思
  • 做网站开发需要学什么软件微信公众平台开发
  • 网站整体营销方案网络营销的特点是什么?
  • 国内知名的网站建设公司有哪些百度指数专业版app
  • 画画外包网站如何推广一个网站
  • 互联网公司响应式网站深圳google推广
  • 深圳网站设计哪好什么推广平台比较好
  • 打开英文网站字体不对教程seo推广排名网站
  • 昭通市建设局网站太原百度关键词优化