java 617.合并两个二叉树(第一个).java
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 617.合并两个二叉树(第一个).java相关的知识,希望对你有一定的参考价值。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
private TreeNode helper(TreeNode t1, TreeNode t2) {
if (t2 == null) return t1;
if (t1 == null) return t2;
t1.val += t2.val;
t1.left = helper(t1.left, t2.left);
t1.right = helper(t1.right, t2.right);
return t1;
}
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
return helper(t1, t2);
}
}
/*
[1,3,2,5]
[2,1,3,null,4,null,7]
[1,2,3]
[1,2,3,4,5,6,7]
[1,2,null,3]
[1,null,2,null,3]
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if(t1 == null) return t2;
if(t2 == null) return t1;
TreeNode left = mergeTrees(t1.left, t2.left);
TreeNode right = mergeTrees(t1.right, t2.right);
t1.val += t2.val;
t1.left = left;
t1.right = right;
return t1;
}
}
以上是关于java 617.合并两个二叉树(第一个).java的主要内容,如果未能解决你的问题,请参考以下文章
java 617.合并两个二叉树(第一个).java
java 617.合并两个二叉树(第一个).java
java 617.合并两个二叉树(第一个).java
java 617.合并两个二叉树(第一个).java
我用java刷 leetcode 617.合并二叉树
617-合并二叉树