653. Two Sum IV - Input is a BST
问题
Given the root of a Binary Search Tree and a target number k, return true if there exist two elements in the BST such that their sum is equal to the given target.
DFS搜索,每次递归时检查HashSet中是否有当前节点的值。如没有则将目标值减去当前节点的值加入HashSet。如有则返回true。
递归左侧节点和右侧节点,并返回二者的或运算。
1 | /** |
653. Two Sum IV - Input is a BST
https://xuanhe95.github.io/2022/04/16/653-Two-Sum-IV-Input-is-a-BST/