Thanks for sharing your solution! Quite an interesting challenge.
I tried to solve it using Haskell since I'm currently learning the language, and after a few mistakes I ended up with the following code:
data Tree a = Empty | Node a (Tree a) (Tree a) deriving (Show, Eq) isUnival :: (Eq a) => Tree a -> Bool isUnival tree = case tree of Empty -> False Node v l r -> case (l, r) of (Node lv _ _, Node rv _ _) -> v == lv && v == rv && isUnival l && isUnival r (Node lv _ _, Empty) -> v == lv && isUnival l (Empty, Node rv _ _) -> v == rv && isUnival r (Empty, Empty) -> True univalCount :: (Eq a) => Tree a -> Int univalCount tree = case tree of Empty -> 0 Node _ l r -> if isUnival tree then 1 + childCount else childCount where childCount = univalCount l + univalCount r
As you can see, it's pretty short, and for me it looks very readable. Have a nice day!
If there's one thing Java ain't, it's succinct. Great solution!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Thanks for sharing your solution! Quite an interesting challenge.
I tried to solve it using Haskell since I'm currently learning the language, and after a few mistakes I ended up with the following code:
As you can see, it's pretty short, and for me it looks very readable.
Have a nice day!
If there's one thing Java ain't, it's succinct. Great solution!