当前位置:网站首页>700. Search DFS method in binary search tree
700. Search DFS method in binary search tree
2022-07-24 17:38:00 【Mr Gao】
700. Search in binary search tree
Given a binary search tree (BST) The root node root And an integer value val.
You need to BST The node value found in is equal to val The node of . Return the subtree with this node as the root . If the node doesn't exist , Then return to null .
Example 1:
Input :root = [4,2,7,1,3], val = 2
Output :[2,1,3]
Example 2:
Input :root = [4,2,7,1,3], val = 5
Output :[]
The solution code is as follows :
/** * 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;
}
边栏推荐
- The most powerful programmer on the earth is equipped with a "three piece set". Do you know what it is?
- Baidu PaddlePaddle easydl x wesken: see how to install the "eye of AI" in bearing quality inspection
- Tensorflow introductory tutorial (37) -- DC Vnet
- Canvas 从入门到劝朋友放弃(图解版)
- Image information is displayed by browser: data:image/png; Base64, + image content
- Introduction and use of Pinia
- Detailed explanation of ansible automatic operation and maintenance (V) the setting and use of variables in ansible, the use of jinja2 template and the encryption control of ansible
- Link editing tips of solo blog posts illegal links
- Transformer structure analysis -- learning notes
- DHCP relay of HCNP Routing & Switching
猜你喜欢

The most powerful programmer on the earth is equipped with a "three piece set". Do you know what it is?

How the computer accesses the Internet (IV) LAN and server response

Internship report 1 - face 3D reconstruction method

Yolopose practice: one-stage human posture estimation with hands + code interpretation

Coldplay weekly issue 10

Portmap port forwarding

二维卷积——torch.nn.conv2d的使用

Image information is displayed by browser: data:image/png; Base64, + image content

The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced

Use Matplotlib to simulate linear regression
随机推荐
hcip第三天
JS image conversion Base64 Base64 conversion to file object
CDN (content delivery network) content distribution network from entry to practice
It's time to consider slimming down your app
Wallys/3 × 3/2 × 2 MIMO 802.11ac Mini PCIe Wi-Fi Module, Dual Band, 2,4GHz / 5GHz/QCN9074
C # print reports using fastreport.net
Mobile robot (IV) four axis aircraft
2022 ranking list of database audit products - must see!
获取同程(艺龙)酒店数据
Array double pointer - deliberate practice
电脑监控是真的吗?4个实验一探究竟
2022年最新浙江建筑安全员模拟题库及答案
Eth POS 2.0 stacking test network pledge process
NPM install reported -4058 error
Array learning navigation
Stop littering configuration files everywhere! Try our 7-year-old solution, which is stable
[waiting for insurance] what does waiting for insurance rectification mean? What are the rectification contents?
2022 牛客暑期多校 K - Link with Bracket Sequence I(线性dp)
Getaverse,走向Web3的远方桥梁
Canvas 从入门到劝朋友放弃(图解版)