修改: 0617 合并二叉树

修改代码细节,优化性能.

用原有的Root1 代替 创建的NewRoot.
This commit is contained in:
AronJudge
2022-07-16 15:43:50 +08:00
parent c75fdf3dae
commit 8b9b64d7d5

View File

@ -262,10 +262,10 @@ class Solution {
if (root1 == null) return root2;
if (root2 == null) return root1;
TreeNode newRoot = new TreeNode(root1.val + root2.val);
newRoot.left = mergeTrees(root1.left,root2.left);
newRoot.right = mergeTrees(root1.right,root2.right);
return newRoot;
root1.val += root2.val;
root1.left = mergeTrees(root1.left,root2.left);
root1.right = mergeTrees(root1.right,root2.right);
return root1;
}
}
```