문제풀이 2021 03 14
2021.03.14 연습
- 사용언어 : java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
TreeNode result = new TreeNode();
if (t1 == null && t2 == null) return null;
if (t1 != null && t2 == null) return t1;
if (t1 == null && t2 != null) return t2;
result.val = t1.val + t2.val;
result.left = mergeTrees(t1.left, t2.left);
result.right = mergeTrees(t1.right, t2.right);
return result;
}
}
Runtime: 1 ms, faster than 36.65% of Java online submissions for Merge Two Binary Trees. Memory Usage: 48.4 MB, less than 11.93% of Java online submissions for Merge Two Binary Trees.