Press "Enter" to skip to content

How to Morph a Binary Tree From Its Left Subsum

Ajk 0

Today I wanted to do a quick recursive problem that came to my attention. It took me about 20 mins to finalize this the way I wanted it… (5-10 too many). So basically here we are asked to morph a given binary tree so that every node in the tree will be equal to all the nodes on its left plus itself. Smells like tree traversal! And whenever we have tree traversals we can mostly use recursion to cover our cases. Below is the code!

How to Morph a Binary Tree From Its Left Subsum

Basically, we keep track of the previousLeftSum. Then we do an inorder traversal.
Go left first, updated leftmost nodes, then update the node we’re looking at. At the end update the rightmost node.

Great! You probably know how to transform trees now and how to do tree traversals.

Hope you guys enjoyed it… And I’ll see you guys next time 😉

The following two tabs change content below.
If you like one of my posts the best way to support is give it a thumbs up, comment, or share it on social media 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *