Two Sum IV - Input is a BST – LeetCode 653
Problem
Description
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.
Answer
Original
Code
1 | /** |
思路
简单的BFS查找,使用set进行统计。时间复杂度$O(n)$,空间复杂度$O(n)$。
耗时$24$ ms,排名$33.13\%$
Better
思路
使用BFS优化递归。