We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 82e9776 commit 0facc21Copy full SHA for 0facc21
javascript/617-Merge-Two-Binary-Trees.js
@@ -0,0 +1,20 @@
1
+/**
2
+ * @param {TreeNode} root1
3
+ * @param {TreeNode} root2
4
+ * @return {TreeNode}
5
+ * Time complexity = O(n+m)
6
+ */
7
+ var mergeTrees = function(root1, root2) {
8
+ // Base case to return null as result of having both root1, root2 null
9
+ if(!root1 && !root2) {
10
+ return null;
11
+ }
12
+
13
+ const val1 = root1 ? root1.val : 0;
14
+ const val2 = root2 ? root2.val : 0;
15
16
+ const root = new TreeNode(val1+val2);
17
+ root.left = mergeTrees(root1 ? root1.left : null, root2 ? root2.left: null);
18
+ root.right = mergeTrees(root1 ? root1.right : null , root2 ? root2.right: null);
19
+ return root;
20
+};
0 commit comments