Welcome to my personal website. I use this space to share some of my thoughts and software that I write in my free time. Sometimes I also publish photos. If you want to get in touch please use LinkedIn or Mastodon.
Given a binary tree, perform pre-order traversal and flatten it to a linked list such that right for each node is the next node.
In the naive solution O(n) space complexity, we just perform a pre-order traversal (current node, left, right) through the tree and add every node as we visit them. Then in a final pass, set the left to None for each node and right to the next node in the list - except for the last node where right stays None.
This can be simplified using Morris traversal:
About the author
Friedrich Ewald is an experienced Software Engineer with a Master's degree in Computer Science. He started this website in late 2015, mostly as a digital business card. He is interested in Go, Python, Ruby, SQL- and NoSQL-databases, machine learning and AI and is experienced in building scalable, distributed systems and micro-services at multiple larger and smaller companies.