'''
keys: 
Solutions:
Similar:
T:
S:
'''
from typing import List


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
        
        
class Solution:
    
    # recursion
    def findTarget1(self, root: TreeNode, k: int) -> bool:
        
        def dfs(node):
            if not node:
                return False
            if node.val in diff:
                return True
            diff.add(k - node.val)
            return dfs(node.left) or dfs(node.right)
        
        diff = set()
        return dfs(root)
        
        
    
    # self-made dfs; O(N) for S and T
    def findTarget(self, root: TreeNode, k: int) -> bool:
        if not root:
            return False
        
        stack = [root]
        diff = set()
        
        while stack:
            node = stack.pop()
            if node:
                if node.val in diff:
                    return True
                else:
                    diff.add(k - node.val)
                if node.left:
                    stack.append(node.left)
                if node.right:
                    stack.append(node.right)
        