Skip to content

Commit 8f7df24

Browse files
committed
add LC-617
Signed-off-by: Tahsin Tunan <[email protected]>
1 parent ee7a3bc commit 8f7df24

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

go/0617-merge-two-binary-trees.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
10+
if root1 == nil && root2 == nil {
11+
return nil
12+
}
13+
if root1 == nil {
14+
return root2
15+
}
16+
if root2 == nil {
17+
return root1
18+
}
19+
20+
var node TreeNode
21+
node.Val = root1.Val + root2.Val
22+
node.Left = mergeTrees(root1.Left, root2.Left)
23+
node.Right = mergeTrees(root1.Right, root2.Right)
24+
25+
return &node
26+
}

0 commit comments

Comments
 (0)