Intuition
Given two binary trees, merge them into a single tree and return the root.
They have also provided the rules of merging. Well, whenever two nodes coincide, add the values of both the nodes and attach it to the new tree. If they do not coincide, create a new one.
So if we have both t1 and t2, then build the tree as usual, but when both or either one is None, then return t1 or t2
. Ideally, this is not the best design, since we are attaching references to the input tree in our result tree. In a scenario where the input trees are destroyed, our result tree would have holes in it.
Follow up: Can you structure it such that a new TreeNode is attached and not a reference to the input trees.
The code for this is quite tricky to get one’s head around atleast for some time - it sure was for me haha.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is where
N == number of nodes in t1
andM == number of nodes
in t2. -
Memory
The memory usage is since we are not using any extra data structure.
— A