Merge branch 'master' into patch-21
This commit is contained in:
@@ -256,23 +256,23 @@ public:
|
||||
|
||||
|
||||
Java:
|
||||
|
||||
```Java
|
||||
class Solution {
|
||||
// 递归
|
||||
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
|
||||
if (root1 == null) {
|
||||
return root2;
|
||||
}
|
||||
if (root2 == null) {
|
||||
return root1;
|
||||
}
|
||||
root1.val += root2.val;
|
||||
root1.left = mergeTrees(root1.left, root2.left);
|
||||
root1.right = mergeTrees(root1.right, root2.right);
|
||||
return root1;
|
||||
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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```Java
|
||||
class Solution {
|
||||
// 迭代
|
||||
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
|
||||
@@ -323,4 +323,4 @@ Go:
|
||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||
* 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ)
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
<div align="center"><img src=../pics/公众号.png width=450 alt=> </img></div>
|
||||
Reference in New Issue
Block a user