Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions go/0617-merge-two-binary-trees.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func mergeTrees(root1 *TreeNode, root2 *TreeNode) *TreeNode {
if root1 == nil && root2 == nil {
return nil
}
if root1 == nil {
return root2
}
if root2 == nil {
return root1
}

var node TreeNode
node.Val = root1.Val + root2.Val
node.Left = mergeTrees(root1.Left, root2.Left)
node.Right = mergeTrees(root1.Right, root2.Right)

return &node
}