Leetcode in Java && Oracle
421 subscribers
8 photos
397 files
400 links
Second channel: @codeforces_java

Let's Develop Together!
Download Telegram
image_2021-11-30_16-14-54.png
36.1 KB
#N617. Merge Two Binary Trees
problem link

#solution
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if(root1 == null && root2 == null) return null;
else if(root1 == null) return root2;
else if(root2 == null) return root1;

TreeNode res = new TreeNode(root1.val + root2.val);
res.left = mergeTrees(root1.left, root2.left);
res.right = mergeTrees(root1.right, root2.right);

return res;
}
}