From 21c05df2ba18191aa9f08ab79e70b57eb4336d16 Mon Sep 17 00:00:00 2001 From: Breno Baptista Date: Thu, 24 Jun 2021 21:43:07 -0300 Subject: [PATCH] Added solution 617 --- .../617. Merge Two Binary Trees.go | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 leetcode/0617.Merge-Two-Binary-Trees/617. Merge Two Binary Trees.go diff --git a/leetcode/0617.Merge-Two-Binary-Trees/617. Merge Two Binary Trees.go b/leetcode/0617.Merge-Two-Binary-Trees/617. Merge Two Binary Trees.go new file mode 100644 index 00000000..733e3f7f --- /dev/null +++ b/leetcode/0617.Merge-Two-Binary-Trees/617. Merge Two Binary Trees.go @@ -0,0 +1,33 @@ +package leetcode + +import ( + "github.com/halfrost/LeetCode-Go/structures" +) + +// TreeNode define +type TreeNode = structures.TreeNode + +/** + * 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 { + return root2 + } + + if root2 == nil { + return root1 + } + + root1.Val += root2.Val + root1.Left = mergeTrees(root1.Left, root2.Left) + root1.Right = mergeTrees(root1.Right, root2.Right) + + return root1 +}