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 ee7a3bc commit 8f7df24Copy full SHA for 8f7df24
go/0617-merge-two-binary-trees.go
@@ -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