Skip to content

Instantly share code, notes, and snippets.

@evgenii-malov
Created December 10, 2021 14:34
Show Gist options
  • Select an option

  • Save evgenii-malov/a1d866ddfed13c97c0c103076dc1ec74 to your computer and use it in GitHub Desktop.

Select an option

Save evgenii-malov/a1d866ddfed13c97c0c103076dc1ec74 to your computer and use it in GitHub Desktop.
Left and right view of binary tree
-- subscribe my channel https://www.youtube.com/channel/UCBGYk5s1Fnk0PpqSwtH1M9w
-- https://gist.github.com/evgenii-malov/1fc29a652751451dbca0d54d454cc1ef
import PrettyT
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
paths :: Btree a -> [[a]]
paths Empty = []
paths (Node a Empty Empty) = [[a]]
paths (Node a l r) = (a:) <$> ((paths l)++(paths r))
maskm x y = x ++ drop (length x) y
lv t = foldl maskm [] $ paths t
rv t = foldr (flip maskm) [] $ paths t
-- --------------------------------------
zip' :: [[a]] -> [[a]] -> [[a]]
zip' l [] = l
zip' [] r = r
zip' (x:xs) (y:ys) = (x++y): zip' xs ys
lo :: Btree a -> [[a]]
lo Empty = []
lo (Node a l r) = [[a]] ++ zip' (lo l) (lo r)
lv' t = head <$> lo t
rv' t = last <$> lo t
@evgenii-malov

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment