Scala recursive solution


  • 0
    K
    object Solution {
        def findTarget(root: TreeNode, k: Int): Boolean = {
            val values = scala.collection.mutable.Set[Int]()
            
            helper(root, k, values)
        }
        
        def helper(node: TreeNode, k: Int, values: scala.collection.mutable.Set[Int]): Boolean = {
            node match {
                case n if n == null => false
                case s if values.contains(k - s.value) => true
                case r => 
                    values += r.value
                    helper(r.left, k, values) || helper(r.right, k, values)
            }
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.