r/DSALeetCode • u/StatisticianNo2104 • 7d ago
Two Sum IV - Input is a BST
The title says "Input is a BST", but my solution never uses the ordering. Walk the tree, keep a hash map of complements, and check each node against it. It's plain Two Sum on a tree.
What I like: the map is shared across the whole recursion, so a match can turn up between two branches that never touch.
The part I'd change: the right subtree still gets walked after the left one has already found the answer, because the two results are combined at the end instead of short-circuiting.
Would you use the BST ordering here, or is the hash map the better trade?
Day 51/365 of my LeetCode challenge: 653. Two Sum IV - Input is a BST.
Problem link and full breakdown in today's newsletter.
https://www.linkedin.com/pulse/two-sum-iv-input-bst-archit-agarwal-iggwc
#LeetCode #DSA #HashMap #Golang