Simple Scala Solution in 8 lines


  • 0
    Z
    def pathSum(root: TreeNode, sum: Int): List[List[Int]] = {
      if (root!=null && root.left==null && root.right==null && sum==root.value) return List(List(sum))
      if (root!=null && root.left==null && root.right==null && sum!=root.value) return List()
      if (root==null) return List()
      val childSum = sum - root.value
      val preLeftResult = pathSum(root.left,childSum)
      val preRightResult= pathSum(root.right,childSum)
      val preResult = preLeftResult:::preRightResult
      preResult.map(root.value::_)
    }
    

Log in to reply
 

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