这是悦乐书的第284次更新,第301篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第152题(顺位题号是669)。给定二叉搜索树以及L和R的最低和最高边界,修剪树以使其所有元素位于[L,R](R> = L)。可能需要更改树的根,因此结果应返回修剪后的二叉搜索树的新根。例如:

输入:L = 1 R = 2

    1
/ \
0 2

输出:

     1
\
2

输入:L = 1 R = 3

    3
/ \
0 4
\
2
/
1

输出:

      3
/
2
/
1

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

使用递归的方式。因为是二叉搜索树,并且返回的新二叉树的根节点值是未定的,需要先确定根节点在哪,确定完根节点后,再去确定它的左子树和右子树。如果当前节点值比右边界要大,那么新节点只可能在左子树那边;如果当前节点值比左边界还要小,那么新节点只能在右子树那边。剩下就是正常情况,新左节点还是在左边,新右节点还是在右边。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode trimBST(TreeNode root, int L, int R) {
if (root == null) {
return null;
}
if (root.val > R) {
return trimBST(root.left, L, R);
}
if (root.val < L) {
return trimBST(root.right, L, R);
}
root.left = trimBST(root.left, L, R);
root.right = trimBST(root.right, L, R);
return root;
}
}

03 第二种解法

使用迭代的方式。首先,还是需要确定根节点在哪里,使用一个循环来判断,如果当前节点值大于右边界,就往左子树里面找,如果小于左边界,就往右子树里面找。接着,从确定好的根节点开始,处理它的左子树,还是还是用循环,如果当前节点不为空,并且它的左节点本身也不为空,它的左节点值小于左边界,那么就跟换它的左节点,将当前节点的左节点重新指向到当前节点左节点的右子节点。同理,对于右子树的处理也是一样的逻辑,只是将判断换成了大于右边界值。最后返回新的二叉树。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode trimBST(TreeNode root, int L, int R) {
if (root == null) {
return null;
}
// 先确定根节点的位置
while (root.val > R || root.val < L) {
if (root == null) {
return null;
}
if (root.val > R) {
root = root.left;
}
if (root.val < L) {
root = root.right;
}
}
// 处理左子树
TreeNode temp = root;
while (temp != null) {
// 如果当前节点的左节点不为空且节点值小于左边界
while (temp.left != null && temp.left.val < L) {
// 就将当前节点的左节点指向当前节点左节点的右子节点
temp.left = temp.left.right;
}
temp = temp.left;
}
// 处理右子树
temp = root;
while (temp != null) {
// 如果当前节点的右节点不为空且节点值大于右边界
while (temp.right != null && temp.right.val > R) {
// 就将当前节点的右节点指向当前节点右节点的左子节点
temp.right = temp.right.left;
}
temp = temp.right;
}
return root;
}
}

04 小结

算法专题目前已日更超过四个月,算法题文章152+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode算法题-Trim a Binary Search Tree(Java实现)的更多相关文章

  1. 【LeetCode】669. Trim a Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  2. leetcode算法:Trim a Binar Search Tree

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

  3. LeetCode算法题-Merge Two Binary Trees(Java实现)

    这是悦乐书的第274次更新,第290篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第142题(顺位题号是617).提供两个二叉树,将其合并为新的二叉树,也可以在其中一个二 ...

  4. LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)

    669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...

  5. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  6. 【Leetcode_easy】669. Trim a Binary Search Tree

    problem 669. Trim a Binary Search Tree 参考 1. Leetcode_easy_669. Trim a Binary Search Tree; 完

  7. Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees

    Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees 669.Trim a Binary Search Tr ...

  8. [LeetCode] Trim a Binary Search Tree 修剪一棵二叉搜索树

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

  9. LeetCode - Trim a Binary Search Tree

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

随机推荐

  1. Vim 复制粘帖格式错乱问题的解决办法

    有时候,复制文本(尤其是代码)到 Vim,会出现格式错乱的问题.看样子,应该是自动缩进惹得祸.本文不去深究原因,直接给出解决方法. 1. paste 模式 运行如下命令,进入 paste 模式: :s ...

  2. Spring Boot2.0 设置拦截器

    所有功能完成 配置登录认证 配置拦截器 在spring boot2.0 之后 通过继承这个WebMvcConfigurer类 就可以完成拦截 新建包com.example.interceptor; 创 ...

  3. leetcode — decode-ways

    /** * Source : https://oj.leetcode.com/problems/decode-ways/ * * * A message containing letters from ...

  4. 记一个常见的ms sql server中取第N条记录的方法

    前言 好好学习,天天向上. 正文 好像也是一个不难的问题,刚视频里看到的,就记一下吧. 下面是表中原始的数据结构,做了一个倒叙排序: select * from Employee order by S ...

  5. Docker公共&本地镜像仓库(七)--技术流ken

    分发镜像 我们已经会构建自己的镜像了,那么如果在多个docker主机上使用镜像那?有如下的几种可用的方法: 用相同的Dockerfile在其他host上构建镜像 将镜像上传到公共registry(比如 ...

  6. [Redux] redux之combineReducers

    combineReducers combineReducer 是将众多的 reducer 合成通过键值映射的对象,并且返回一个 combination 函数传入到 createStore 中 合并后的 ...

  7. Java高并发--原子性可见性有序性

    Java高并发--原子性可见性有序性 主要是学习慕课网实战视频<Java并发编程入门与高并发面试>的笔记 原子性:指一个操作不可中断,一个线程一旦开始,直到执行完成都不会被其他线程干扰.换 ...

  8. mysql查找字段在哪个表中

    select table_schema 数据库名称,table_name  表名 from information_schema.columns where column_name = 'compar ...

  9. css transition 实现滑入滑出

    transition是css最简单的动画. 通常当一个div属性变化时,我们会立即看的变化,从旧样式到新样式是一瞬间的,嗖嗖嗖!!! 但是,如果我希望是慢慢的从一种状态,转变成另外一种状态,怎么办?  ...

  10. Windows中通过命令行新建文件夹、新建文件,和一些常用命令

    新建文件 和Linux不太一样,Linux中的touch和vi命令创建新文件的方法都不能用了,在windows命令行下得用type nul>文件名.后缀名来创建: F:\study\vue\wo ...