The above code isn't exactly right though-- we're not returning a sum of the two values after checking for null on both, but rather we should be returning a merged node object. To save space, if there are values at both nodes, let's just add them onto the first tree and simply return it.
xxxxxxxxxx76
'PASSED: `mergeTwoBinaryTrees(tree1, tree2)` should return a merged tree'var assert = require('assert');function Node(val) { this.val = val; this.left = this.right = null;}function mergeTwoBinaryTrees(tree1, tree2) { // Fill in this method 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);tree2.left = new Node(10);tree2.left.left = new Node(17);tree2.left.right = new Node(3);OUTPUT
Results will appear here.