Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0facc21

Browse files
committed
617. Merge Two Binary Trees
1 parent82e9776 commit0facc21

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
*@param {TreeNode} root1
3+
*@param {TreeNode} root2
4+
*@return {TreeNode}
5+
* Time complexity = O(n+m)
6+
*/
7+
varmergeTrees=function(root1,root2){
8+
// Base case to return null as result of having both root1, root2 null
9+
if(!root1&&!root2){
10+
returnnull;
11+
}
12+
13+
constval1=root1 ?root1.val :0;
14+
constval2=root2 ?root2.val :0;
15+
16+
constroot=newTreeNode(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+
returnroot;
20+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp