原题

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

解析

合并两个2叉树

给两个二叉树,合并的规则是:

如果两个树相同位置的节点存在,则将节点值相加作为新节点的值

如果该位置只有一棵树有节点,则将这个节点作为合并后的树相应位置的节点

Input:

Tree 1 Tree 2

1 2

/ \ /

 3 2 1 3

/ \

5 4 7

Output:

Merged tree:

3

/

4 5

/ \

5 4 7

我的解法

public class Merge2BinaryTree {
public static TreeNode getMergedTree(TreeNode firstNode, TreeNode secondNode) {
if (firstNode == null && secondNode == null) {
return null;
}
TreeNode mergedTree = new TreeNode(0);
if (firstNode != null) {
mergedTree.setValue(mergedTree.getValue() + firstNode.getValue());
}
if (secondNode != null) {
mergedTree.setValue(mergedTree.getValue() + secondNode.getValue());
}
mergedTree.setLeftNode(
getMergedTree(firstNode == null ? null : firstNode.leftNode,
secondNode == null ? null : secondNode.leftNode));
mergedTree.setRightNode(
getMergedTree(firstNode == null ? null : firstNode.rightNode,
secondNode == null ? null : secondNode.rightNode));
return mergedTree;
}
}
/**
* 树的节点类,里面有该节点的值
* 以及左子节点,右子节点
*/
class TreeNode {
int value;
TreeNode leftNode;
TreeNode rightNode; public TreeNode(int v) {
this.value = v;
} public int getValue() {
return value;
} public void setValue(int value) {
this.value = value;
} public TreeNode getLeftNode() {
return leftNode;
} public void setLeftNode(TreeNode leftNode) {
this.leftNode = leftNode;
} public TreeNode getRightNode() {
return rightNode;
} public void setRightNode(TreeNode rightNode) {
this.rightNode = rightNode;
}
}

最优解

public class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null) return null; int val = (t1 == null ? 0 : t1.val) + (t2 == null ? 0 : t2.val);
TreeNode newNode = new TreeNode(val); newNode.left = mergeTrees(t1 == null ? null : t1.left, t2 == null ? null : t2.left);
newNode.right = mergeTrees(t1 == null ? null : t1.right, t2 == null ? null : t2.right); return newNode;
}
}

思路其实一样,只是他用了三目表达式,减少了行数

【leetcode】617. Merge Two Binary Trees的更多相关文章

  1. 【LeetCode】617. Merge Two Binary Trees 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  2. 【Leetcode_easy】617. Merge Two Binary Trees

    problem 617. Merge Two Binary Trees     参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完    

  3. 【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  4. 【leetcode】951. Flip Equivalent Binary Trees

    题目如下: For a binary tree T, we can define a flip operation as follows: choose any node, and swap the ...

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees

    Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees 669.Trim a Binary Search Tr ...

  7. leetcode第一天-merge two binary trees

    有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...

  8. [LeetCode] 617. Merge Two Binary Trees 合并二叉树

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  9. LeetCode 617 Merge Two Binary Trees 解题报告

    题目要求 Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...

随机推荐

  1. Jmeter之分布式部署测试

    在使用Jmeter进行性能测试时,因受单机电脑的配置限制,可能无法支持较大数量的并发,此时就需要使用Jmeter提供的分布式测试的功能. jmeter分布式测试的执行原理是选择一台作为调度机,其他机器 ...

  2. uni-app 使用Vuex+ (强制)登录

    一.在项目的根目录下新建一个store文件夹,然后在文件夹下新建一个index.js文件 二.在新建的index.js下引入vue和vuex,具体如下: //引入vue和vuex import Vue ...

  3. mysql访问慢解决

    配置变更思路: 扩大MySQL连接数至2000,同时扩大操作系统最大文件描述符:扩大innodb缓存池 操作步骤: vi /etc/my.cnf max_connections = 2000innod ...

  4. Jmeter 逻辑控制器 之 ForEach 控制器

    一.认识 ForEach 控制器 如下,创建一个 ForEach 控制器 设置界面如下: 输入变量前缀:要进行循环读取的变量前缀 Start index for loop (exclusive):循环 ...

  5. webdriver(处理select下拉框元素)

    """处理下拉框""" from selenium import webdriver from selenium.webdriver.com ...

  6. java.net.UnknownHostException: MySQLMASTER: MySQLMASTER: 未知的名称或服务

    linux环境在连接Activemq的时候报以下信息,找了半天配了下host  OK了,记录一下. java.net.UnknownHostException: MySQLMASTER: MySQLM ...

  7. gdb调试工具学习

    GDB 是GNU开源组织发布的一个强大的UNIX下的程序调试工具.或许,各位比较喜欢那种图形界面方式的,像VC.BCB等IDE的调试,但如果你是在 UNIX平台下做软件,你会发现GDB这个调试工具有比 ...

  8. (模板)求逆矩阵luoguP4783

    题目链接:https://www.luogu.org/problem/P4783 题意:求矩阵的逆. 思路:高斯消元法求矩阵的逆,n为400,卡常,我是开了O2优化才AC的.. AC代码: #incl ...

  9. 安装matplotlib,报错ERROR: Command errored out with exit status 1:

    使用pip install matplotlib 出现报错信息: 发现这行报错 : 我是在pycharm上安装的,可是提示我去安装 Microsoft Visual C++ ,然后去百度查了下,发现只 ...

  10. IT 界的“名言”

    --喜欢记得关注我哟[shoshana]-- 中国有很多古代警世名言,朗朗上口,凝聚了很多故事与哲理.硅谷的互联网公司里头也有一些这样的名言,凝聚了很多公司价值观和做事的方法,对于很多程序员来说,其影 ...