One Pager Cheat Sheet
- The sum of overlapping nodes is used to create the merged tree, while nodes that do not overlap remain unchanged, within the constraints of
O(m+n)time complexity and-1000000000to1000000000for node values. - We can
merge treeswith just root nodes togaugehow we'dperformthis exercise fortrees with children. - We can
brute forcesolve a problem byvisualizinghow a human would do it, and running through examples to find patterns. - The merge rule states that when two nodes overlap, their attributes and properties are combined
summarilyto form a resultant node. - The merge rule allows any two
overlappingnodes to be combined, so it does not have to start from the root nodes of both trees. - We could
sum upthenode valuesof two trees, byprocessing at the rootof each one, and thenchecking for left and right children, according torule #2. - We
scanthe tree from left to right andsum upthe values we find between eachnode, returning4from the root's left child. - The merge tree function
combinesthe twotree nodesbyaddingthe two elements within them together toget the result. - The algorithm of
pre-order traversalis a Depth-First Search which visits the nodes of a tree in the root, left, right order. - We can
traverseboth trees simultaneously andchecktheir values at each step to create a pattern. - The
pre-order traversaltechnique is the key to returning a merged root. - Checking the
node positionof thecurrently investigatedelement at thefirstandsecondtrees usingdoSomeOperation()can determine what toreturn. - We should
mergethe two nodes by adding their values onto the first tree, and then simply return it. - The process of merging two binary trees recursively begins with a pre-order traversal of the first tree, checking the nodes of both trees, and then updating the first tree with the sum of their values before continuing with the subtrees.
- We should
traverseboth trees beforereturningtree1, writing the merge results in the process. - We traverse through
bothbinary trees at once with atime complexityof O(m+n), allowing us to process one node ofeach treeper iteration. - The
time complexityof the recursive tree merger algorithm is O(n), as it only traverses the larger tree, resulting in a worst-case of O(n) nodes.
This is our final solution.
To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.
xxxxxxxxxx83
}var assert = require('assert');​function Node(val) { this.val = val; this.left = this.right = null;}​function mergeTwoBinaryTrees(tree1, tree2) { if (tree1 == null) { return tree2; } if (tree2 == null) { return tree1; } tree1.val += tree2.val; tree1.left = mergeTwoBinaryTrees(tree1.left, tree2.left); tree1.right = mergeTwoBinaryTrees(tree1.right, tree2.right); return tree1;}​function Node(val) { this.val = val; this.left = null; this.right = null;}​// Regular binary treesvar tree1 = new Node(4);tree1.left = new Node(1);tree1.right = new Node(3);​var tree2 = new Node(5);OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
Got more time? Let's keep going.
If you had any problems with this tutorial, check out the main forum thread here.


