[LeetCode] 700. Search in a Binary Search Tree

RobotStudio码垛机器人创建过程 首先导入一个IRB2600机器人,移动夹具的本地坐标原点,使原点位置为顶面中心(与法兰盘连接的部位)。对齐夹爪Smart组件的本地坐标和机器人末端法兰盘的坐标... 阅读详情

原题链接: https://leetcode.com/problems/search-in-a-binary-search-tree/

1. 题目介绍

Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node’s value equals the given value. Return the subtree rooted with that node. If such node doesn’t exist, you should return NULL.

给出一个二叉树的根节点和一个value值。你需要在所有二叉树的节点中找到那个val等于value值的节点。返回以这个节点为根节点的子树。如果没有节点等于value,那么返回null

举例:
在这里插入图片描述
应该返回这个子树:
在这里插入图片描述
In the example above, if we want to search the value 5, since there is no node with value 5, we should return NULL.
在上述的例子中,如果value值等于5,那么这里就没有等于5的节点了。我们应该返回null。

Note that an empty tree is represented by NULL, therefore you would see the expected output (serialized tree format) as [], not null.
一个空树也可以用NULL表示,因此期望输出的结果应该是[],不是null
(其实我不太懂这句话表达的意思是什么,如果有读者知道,欢迎留言告诉我)

2. 解题思路

这道题是一道遍历树的问题。我们需要遍历这棵树,寻找和value值相等的节点。遍历一棵树有很多种办法,其中深度优先搜索是最适合本题的。根据二叉树的性质,这个根节点左子树里面的所有节点都小于根节点,所有右子树里面的节点都大于根节点。如果使用深度优先搜索的话,假如root.val 的值大于value,那么我们可以直接放弃root的右子树,把搜寻范围确定在左子树上,直接降低了一半的搜索工作量。

具体实现仍然采用深度优先搜索+递归的实现方法。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        if(root == null){
            return null;
        }
        if(root.val == val){
            return root;
        }
        
        if(root.val > val){
            return searchBST(root.left,  val);
        }
        if(root.val < val){
            return searchBST(root.right, val);
        }
        
        return null;
    }
}
《Python基础教程》专栏总结篇 今天给大家带来的文章是《Python基础教程》专栏总结篇,希望能对同学们有所帮助。 文章目录 1. 背景 2. 专栏亮点 3. 你的收获 4. 详细目录 阅读详情

相关推荐

时频分析之STFT:短时傅里叶变换的原理与代码实现(非调用Matlab API)

STFT function [STFT, f, t] = mystft(x, win, hop, nfft, fs) % function: [STFT, f, t] = stft(x, win, hop, nfft, fs) % % Input: % x - signal in the time domain % win - analysis window function % hop - hop size % nfft - numb

frostime的博客 13万+

【LeetCode】700. Search in a Binary Search Tree 解题报告(Python)

【LeetCode】700. Search in a Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/s...

负雪明烛 3592

FI 应计对象的创建和摊销

创建/修改:ACACTREE01/ACACTREE02 应计对象的过账:ACACPOSTING

nancyln9711的博客 906

Search in a Binary Search Tree--LeetCode700 java

Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node’s value equals the given value. Return the subtree rooted with that node. If such node...

USTC_Lucas 201

手机安装Kali Linux

在数字化时代,信息安全和隐私保护显得尤为重要。,作为一款专业的和工具,因其强大的功能和丰富的资源库而受到广大安全研究者和爱好者的青睐。然而,我们通常只能在传统的电脑设备上安装和使用Kali Linux。那么,有没有可能将也变成一款随身携带的Kali Linux设备呢?答案是肯定的。本文将介绍如何在手机上安装Kali Linux,使你的手机变成一款强大的移动安全工具。,我们需要明确一点,手机安装Kali Linux并非简单的操作,需要对手机进行一定的改造和配置。

2301_80124151的博客 6977

CTFShow萌新区全攻略:从密码破解到Web渗透的实战解析

本文为CTFShow萌新区新手提供从密码破解到Web渗透的完整实战攻略。文章深入解析了密码学、隐写术、杂项和Web安全四大核心方向,分享了实用的解题工具、思维方法和绕过技巧,旨在帮助初学者建立系统的安全竞赛解题体系,而不仅仅是提供一份题解(WP)。

weixin_29216957的博客 304

【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python & C++)

【LeetCode】701. Insert into a Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems...

负雪明烛 2227

保姆级教程:在华为云Ubuntu服务器上快速搭建RAGflow开发环境(避坑指南)

本文提供了一份在华为云Ubuntu服务器上快速搭建RAGflow开发环境的保姆级教程。针对网络拉取镜像等常见问题,详细介绍了从服务器选型、系统优化、Docker配置到RAGflow部署与验证的全流程,并给出生产环境调优建议,帮助开发者高效避坑,快速构建企业级应用环境。

weixin_29250403的博客 88

700. Search in a Binary Search Tree

Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node...

学习应该是一个长链接 630

MATLAB算法实战应用案例精讲-【数模应用】梯度提升决策树(GDBT)(附python代码)

GBDT(Gradient Boosting Decision Tree) 又叫 MART(Multiple Additive Regression Tree),是一种迭代的决策树算法,该算法由多棵决策树组成,它在被提出之初就和SVM一起被认为是泛化能力(generalization)较强的算法。近些年更因为被用于搜索排序的机器学习模型而引起大家关注。.........

qq_36130719的博客 2082

YOLOv8改进策略【注意力机制篇】| CVPRW-2024 分层互补注意力混合层 H-RAMi 针对低质量图像的特征提取模块

许多证据表明层次化网络对图像恢复(IR)任务通常不太有效,因为IR的目标是逐个预测像素值(密集预测),而缩小特征图会丢失重要的像素级信息。然而,层次化结构有降低时间复杂度以及学习语义级和像素级特征表示的优点。为了弥补缺点并利用优点,设计了H - RAMi层。

Limiiiing的博客 381

leetcode 669. Trim a Binary Search Tree

1.题目Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the re

沐沐余风的专栏 1213

W5500以太网&PoE扩展模块原理图设计,已量产

本文将以 W5500 以太网 & PoE 扩展板为对象,从嵌入式硬件设计的视角,深度拆解其电路架构、核心器件选型、关键功能实现,以及这款板子解决行业痛点的设计思路。

不脱发的程序猿 987

LeetCode 700. Search in a Binary Search Tree 题解

本题是二叉搜索树的经典问题,主要考察对二叉搜索树性质的理解和应用。通过使用递归或迭代,我们可以高效地在二叉搜索树中搜索目标值。递归的核心思想是:利用二叉搜索树的性质,递归地搜索目标值。迭代的核心思想是:使用循环的方式,利用二叉搜索树的性质搜索目标值。在实际应用中,迭代方法通常更受欢迎,因为它的空间复杂度更低,而且避免了递归调用的栈开销。这种方法不仅适用于二叉搜索树中的搜索问题,还可以应用于许多其他需要在二叉搜索树中查找元素的场景。掌握这些方法,对于解决这类问题非常重要。

cannonjinx的博客 1576

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

LeetCode 700: Search in a Binary Search Tree

目录LeetCode 700: Search in a Binary Search Tree题目描述解题 LeetCode 700: Search in a Binary Search Tree 题目描述 给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。 例如, 给定二叉搜索树: 4 / \ 2 7 / \ 1 3 和值: 2 你应该返回如下子树: 2 / \

s_Shune 137

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Leetcode学习笔记:#700. Search in a Binary Search Tree

Leetcode学习笔记:#700. Search in a Binary Search Tree Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node’s value equals the given value. Retu...

ccystewart的博客 149

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

leetcode 98. Validate Binary Search Tree和700. Search in a Binary Search Tree

【代码】leetcode 98. Validate Binary Search Tree和700. Search in a Binary Search Tree。

weixin_41554427的博客 315

LeetCode 700. Search in a Binary Search Tree

Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node...

wuqingdeqing的博客 170

LeetCode //C - 700. Search in a Binary Search Tree

【代码】LeetCode //C - 700. Search in a Binary Search Tree。

Made in Code 1090

leetcode700 Search in a Binary Search Tree

//递归 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; *...

无知的博客 136

Leetcode 700. Search in a Binary Search Tree(递归)

Leetcode 700. Search in a Binary Search Tree 题目链接: Search in a Binary Search Tree 难度:Easy 题目大意: 在二分搜索树(BST)中查找值为val的节点,返回以该节点为根节点的二叉树,如果找不到则返回null。 思路: 递归,先对根节点进行判断,然后对其子节点进行相同的操作。 代码 /** * Definition for a binary tree node. * public class TreeNode { *

xue1236的博客 213

[leetcode]700. Search in a Binary Search Tree

[leetcode]700. Search in a Binary Search Tree Analysis 一个有台风的周末~—— [中午吃什么外卖呢~] Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the no...

weixin_32135877的博客 439

[Leetcode]700. Search in a Binary Search Tree

700. Search in a Binary Search Tree 本题难度: Easy Topic: Binary Tree Description Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's valu...

weixin_30666753的博客 106

Leetcode 700. Search in a Binary Search Tree

【代码】Leetcode 700. Search in a Binary Search Tree。

小白菜又菜 462

Leetcode 700 Search in a Binary Search Tree

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { pub...

Polanwind 141

LeetCode六月挑战(6.15) Search in a Binary Search Tree LeetCode700 解题报告

LeetCode六月挑战(6.15) Search in a Binary Search Tree LeetCode700 解题报告 今天的题目比较简单 Search in a Binary Search Tree Solution Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node’s value equals the given

初七的博客 197
上一篇: [LeetCode] 669. Trim a Binary Search Tree
下一篇: [LeetCode] 144. Binary Tree Preorder Traversal
Ber03
博客等级 码龄9年 7粉丝 169原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值