当前位置:网站首页>Leetcode 653. sum of two IV - input BST
Leetcode 653. sum of two IV - input BST
2022-07-22 18:49:00 【Great white sheep_ Aries】
Title Description
653. Sum of two numbers IV - Input BST
solution : Depth first
Create a hash table , Record the traversed node value , At the same time, find out whether there is and equal to in the hash table k The elements of
/** * 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);
}
};
边栏推荐
猜你喜欢
LeetCode 304. 二维区域和检索 - 矩阵不可变
【SDIO】SD2.0协议分析总结(三)-- SD卡相关命令介绍
【FatFs】基于STM32 SD卡移植FatFs文件系统
PTA 6-11 求自定类型元素序列的中位数 (25 分)
1.常量中有换行符Qt5-》vs的解决方案;2.同一份代码Qt和vs共同编译的问题和解决方案
【 sdio】 résumé de l'analyse du Protocole sd2.0 (Ⅲ) - - Introduction aux commandes pertinentes de la carte SD
【FatFs】FAT32文件系统协议总结(理论+实践)
LeetCode 105. 从前序与中序遍历序列构造二叉树
模块TensorFlow中没有Session
[STM32] STM32 SDIO SD card read / write test (II) -- SD_ Power on phase of init
随机推荐
PAT乙级1010一元多项式求导(题意理解)
Summary 20220118 (binary tree)
【SDIO】SD2.0协议分析总结(一)-- SD卡基本概率介绍
总结20220215(kruskal和prim)
Interview experience of Android Internet manufacturers
【SDIO】SD2.0協議分析總結(三)-- SD卡相關命令介紹
Add the GD Library under centos7.5, and then the MySQL expansion library. There is no problem without the configuration of MySQL expansion. There is no MySQL expansion in phpinfo
[Nordic] nrf52810 OTA upgrade (III) – DFU theoretical analysis
Log4J日志配置详解
LeetCode 105. 从前序与中序遍历序列构造二叉树
Go 并发模式:管道和取消
【Audio】基于STM32 I2S移植WM8978 Audio Codec驱动
Summary 20220208 (KMP)
1.mysql null 和 in;2.127.0.0.2是啥?
【QT源代码复用】QDateTimeEdit的下拉按钮事件响应
总结20220118(二叉树)
总结20220120
01.开闭原则 Open-Closed Principle
总结20220211
Summary 20220119