当前位置:网站首页>700. 二叉搜索树中的搜索-dfs法
700. 二叉搜索树中的搜索-dfs法
2022-07-24 17:31:00 【Mr Gao】
700. 二叉搜索树中的搜索
给定二叉搜索树(BST)的根节点 root 和一个整数值 val。
你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。
示例 1:
输入:root = [4,2,7,1,3], val = 2
输出:[2,1,3]
示例 2:
输入:root = [4,2,7,1,3], val = 5
输出:[]
解题代码如下:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
struct TreeNode* re;
void dfs(struct TreeNode* root,int val){
if(root&&re==NULL){
if(root->val==val){
re=root;
}
dfs(root->left,val);
dfs(root->right,val);
}
}
struct TreeNode* searchBST(struct TreeNode* root, int val){
re=NULL;
dfs(root,val);
return re;
}
边栏推荐
- Is Shenwan Hongyuan securities' low commission account reliable, reliable and safe
- ufw 端口转发
- 地表最强程序员装备“三件套”,你知道是什么吗?
- Getaverse,走向Web3的远方桥梁
- PAT甲级——A + B 格式
- 2022 Niuke summer multi school K - link with bracket sequence I (linear DP)
- Portmap port forwarding
- DF2NET三维模型部署
- Six ways for JS to implement inheritance
- Still using xshell? You are out, recommend a more modern terminal connection tool!
猜你喜欢

Heuristic merging (including examples of general formula and tree heuristic merging)

电脑监控是真的吗?4个实验一探究竟

数论整除分块讲解 例题:2021陕西省赛C

Bring 120W goods in 15 seconds. You can also shoot such a popular video

调整图像亮度的滚动条演示实验

The orders in the same city are delivered in the same city, and the order explosion is still handy!

Rare earth Developer Conference | Apache pulsar committee Liu Dezhi shares the way of cloud native technology transformation

ansible自动化运维详解(五)ansible中变量的设定使用、JINJA2模板的使用以及ansible的加密控制

What is fuzzy theory, foundation and process

Two dimensional convolution -- use of torch.nn.conv2d
随机推荐
通道的分离与合并
2022 牛客暑期多校 K - Link with Bracket Sequence I(线性dp)
Use Matplotlib to simulate linear regression
Work with growingio engineers this time | startdt Hackathon
The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
调整图像亮度的滚动条演示实验
2022 Niuke summer multi school K - link with bracket sequence I (linear DP)
Extension of ES6 function
使用matplotlib模拟线性回归
Wallys/3 × 3/2 × 2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz/QCN9074
Fast power writing
Reptiles and counter crawls: an endless battle
二维卷积——torch.nn.conv2d的使用
数论整除分块讲解 例题:2021陕西省赛C
Tensorflow introductory tutorial (40) -- acunet
Portmap port forwarding
High performance complexity analysis of wechat circle of friends
Pat a - correct spelling
Natbypass port forwarding
详解 Apache Hudi Schema Evolution(模式演进)