Monday, March 29, 2021

Leetcode Everyday: 617. Merge Two Binary Trees. Easy

public class Solution {
    public TreeNode MergeTrees(TreeNode root1, TreeNode root2) =>
        root1 != null || root2 != null ? 
            new TreeNode(
                (root1?.val ?? 0) + (root2?.val ?? 0),
                MergeTrees(root1?.left, root2?.left),
                MergeTrees(root1?.right, root2?.right)            
            )
        : 
            null;                        
}

// TODO: optimize without allocating
Source: https://leetcode.com/problems/merge-two-binary-trees/

No comments:

Post a Comment