Visualizing the Merging Process
The picture truly is worth a thousand words. In this diagram, we see Tree 1 with a root node of 3 and Tree 2 with a root node of 1. As per our observation #1—"Roots Merge"—we combine these roots to create a new root for the merged tree, which has a value of (3 + 1 = 4).
The Steps in the Process
Start at the Roots: We begin with the root nodes of both Tree 1 and Tree 2. These are 3 and 1, respectively.
Merge the Roots: We add the values of these root nodes together. (3 + 1 = 4)
Create the Merged Root: We then use this sum to create the root node of our new, merged tree.
xxxxxxxxxx
/*
1st Tree 2nd Tree Merged Tree
3 1 4
/ \ / \ / \
1 3 4
*/
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment