↓Skip to main content
  1. LeetCode/

LeetCode 653: Two Sum IV - Input is a BST

·2 mins· ·
LeetCode Easy Hash-Table Two-Pointers Tree Depth-First-Search
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-29

  • Submissions: Runtime Error → Accepted ×3 → Wrong Answer → Runtime Error → Accepted

Approach
#

Like Two Sum, keep a set of values seen (Hash Table). At each node, check whether k - val has appeared and return True if so, then add the node’s value. In-order plus two pointers also exploits the BST order, but a set is simplest.

My Attempts
#

Attempt 1 had an undefined variable. My accepted version had an extra if k == 0: return False, but k can be 0 with both -1 and 1 in the tree, so that check is actually wrong and simply was not tested; a November rewrite returned nothing in one version.

Solution
#

class Solution:
    def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
        seen, stack = set(), [root]
        while stack:
            node = stack.pop()
            if not node:
                continue
            if k - node.val in seen:
                return True
            seen.add(node.val)
            stack += [node.left, node.right]
        return False

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Check before adding, or a node pairs with itself.
  • k can be 0 (say -1 and 1); do not treat k == 0 as impossible.

Interview Explanation
#

I traverse the tree with a set of seen values, checking k - val at each node before adding its own value. Time and space O(n).

Related

LeetCode 637: Average of Levels in Binary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search Binary-Tree
LeetCode 637 note: level-order traversal, dividing each level’s sum by its count
LeetCode 671: Second Minimum Node In a Binary Tree
·2 mins
LeetCode Easy Tree Depth-First-Search Binary-Tree
LeetCode 671 note: the root is the minimum; the second minimum is the smallest value larger than the root, and only subtrees equal to the root need searching
LeetCode 559: Maximum Depth of N-ary Tree
·1 min
LeetCode Easy Tree Depth-First-Search Breadth-First-Search
LeetCode 559 note: depth is 1 plus the deepest child, or 1 with no children
LeetCode 563: Binary Tree Tilt
·1 min
LeetCode Easy Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 563 note: return subtree sums in post-order; each node’s tilt is the difference of its subtree sums, added to the answer
LeetCode 572: Subtree of Another Tree
·2 mins
LeetCode Easy Tree Depth-First-Search String-Matching Binary-Tree
LeetCode 572 note: for every node of root, check whether its subtree equals subRoot; recurse with isSubtree, not by comparing only the children
LeetCode 589: N-ary Tree Preorder Traversal
·2 mins
LeetCode Easy Stack Tree Depth-First-Search
LeetCode 589 note: n-ary pre-order records the node then each child in order; flatten recursive results with extend, not by appending a list