Last active
February 25, 2018 12:23
-
-
Save HenriBeck/7fc5160186cfaa77eca2957a7329016d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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