A straightforward Kotlin solution (recursive)

  • 0
    class Solution {
        fun mergeTrees(t1: TreeNode?, t2: TreeNode?): TreeNode? {
            if (t1 == null) return t2
            if (t2 == null) return t1
            val mergedTree = TreeNode(t1.`val` + t2.`val`)
            mergedTree.left = mergeTrees(t1.left, t2.left)
            mergedTree.right = mergeTrees(t1.right, t2.right)
            return mergedTree

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.