Skip to content

Instantly share code, notes, and snippets.

@HenriBeck
Last active February 25, 2018 12:23
Show Gist options
  • Save HenriBeck/7fc5160186cfaa77eca2957a7329016d to your computer and use it in GitHub Desktop.
Save HenriBeck/7fc5160186cfaa77eca2957a7329016d to your computer and use it in GitHub Desktop.
firstDuplicate :: [Int] -> Int
firstDuplicate xs = checkDuplicates xs []
checkDuplicates :: [Int] -> [Int] -> Int
checkDuplicates (x:[]) f = if elem x f then x else -1
checkDuplicates (x:xs) f = if elem x f then x else checkDuplicates xs (x:f)
data Tree a = Tree {
value :: a,
left :: Tree a,
right :: Tree a,
} | Null deriving Show
tree = {
value: 1,
left: {
value: 2,
left: Null,
right: {
value: 4,
left: Null,
right: Null,
}
},
right: {
value: 2,
left: {
value: 3,
left: Null,
right: Null,
},
right: {
value: 3,
left: Null,
right: Null,
}
}
}
hasPathSum :: Tree a -> Int -> Bool
hasPathSum Null _ = False
hasPathSum tree sum
| tree.value == sum = True
| otherwise = (hasPathSum tree.left sum - tree.value) || (hasPathSum tree.right sum - tree.value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment