当前位置:网站首页>LeetCode 653. 两数之和 IV - 输入 BST
LeetCode 653. 两数之和 IV - 输入 BST
2022-07-22 09:17:00 【大白羊_Aries】
题目描述
解法:深度优先
建立一个哈希表,记录遍历过的结点值,同时在哈希表中找是否存在和等于 k 的元素
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
unordered_set<int> hash_table;
bool findTarget(TreeNode* root, int k) {
if (root == nullptr) return false;
if (hash_table.count(k - root->val)) return true;
hash_table.insert(root->val);
return findTarget(root->left, k) || findTarget(root->right, k);
}
};
边栏推荐
- 【FatFs】基于STM32 SD卡移植FatFs文件系统
- Uniapp wechat applet map, open in Gaode app, Tencent app, baidu app, apple map app
- Go 语言学习:Go 语言之旅(四)
- sshfs + autofs + sshpass (by quqi99)
- debug glance(by quqi99)
- 折腾凤凰系统 (by quqi99)
- TCP and UDP, three handshakes and four waves
- Try kolla-ansible (by quqi99)
- creating vlan over openstack (by quqi99)
- 02.依赖导致原则 - Dependence Inversion Principle
猜你喜欢
【SDIO】SD2.0协议分析总结(一)-- SD卡基本概率介绍
周末和技术大咖们聚餐,聊到了软件测试行业的“金九银十”高峰【内卷之势已然形成】
ECSHOP configures wechat payment, and the wechat developer tool prompts "unbound web developer" when wechat payment pops up
[STM32] STM32 SDIO SD card read / write test (II) -- SD_ Power on phase of init
LeetCode: 184. 部门工资最高的员工
Is it really necessary to define VO, Bo, Po, do, dto?
DOM简介及查询
04. interface aggregation principle
01.开闭原则 Open-Closed Principle
TCP and UDP, three handshakes and four waves
随机推荐
ecshop 需要修改权限的文件夹及文件? 报错目录不可写入
总结20220215(kruskal和prim)
总结20220121
sql注入
App移動端測試【6】應用程序(apk)包管理與activity
no input file specified 解决方案
LeetCode: 197. 上升的温度
【QT源代码复用】模拟QCompleter的弹窗方式
Summary 20220208 (KMP)
【 sdio】 résumé de l'analyse du Protocole sd2.0 (Ⅲ) - - Introduction aux commandes pertinentes de la carte SD
使用工厂的方法创建对象
DOM简介及查询
How PHP prevents CSRF attacks
Go 语言学习:Go 语言之旅(三)
07. Composite/aggregate Reuse Principle (carp)
C语言简易TCP服务端程序
H5 no scaling on the mobile terminal
微信公众号网页授权----redirect_uri域名与后台配置不一致,错误码10003 错误
creating vlan over openstack (by quqi99)
No input file specified solution