题解 | #合并二叉树#
合并二叉树
https://www.nowcoder.com/practice/7298353c24cc42e3bd5f0e0bd3d1d759
package main import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param t1 TreeNode类 * @param t2 TreeNode类 * @return TreeNode类 */ func mergeTrees(t1 *TreeNode, t2 *TreeNode) *TreeNode { // write code here if t1 == nil { return t2 } if t2 == nil { return t1 } head := &TreeNode{ Val: t1.Val + t2.Val, } head.Left = mergeTrees(t1.Left, t2.Left) head.Right = mergeTrees(t1.Right, t2.Right) return head }