原题

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. spring boot入门学习---1

    1.maven配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

  2. 打印Linq生成的SQL语句

    var t1 = source.OrderBy<T>(orderby).Skip<T>(_skip).Take<T>(_take); var t2 = t1.ToO ...

  3. JAVA 基础编程练习题32 【程序 32 左移右移】

    32 [程序 32 左移右移] 题目:取一个整数 a 从右端开始的 4-7 位. 程序分析:可以这样考虑: (1)先使 a 右移 4 位. (2)设置一个低 4 位全为 1,其余全为 0 的数.可用~ ...

  4. php-fpm 重启

    查看php-fpm进程数:ps aux | grep -c php-fpm [root@ssy106c14c190c69 etc]# ps -ef | grep php-fpm ---  查看php- ...

  5. mac 查看隐藏文件及快速打开终端

    查看隐藏文件: 1.在目标目录打开终端,然后输入ls -al命令快速查看目标目录下的文件(包括隐藏文件) 2.快捷键shift+cmmand+.(显示或者隐藏) 打开终端方式: 1.设置组合快捷键,单 ...

  6. javascript语法 1.运算符 2. 流程控制 3. 函数 4. 四种变量 5. 数据类型的运用 6. js页面交互

    1.运算符 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...

  7. Spring-Boot的第三方类库的依赖版本调整方法

    springboot方式构建的工程,是dependencyManagement方式进行依赖包的版本管理, spring中有默认的版本,可以修改ext参数来调整版本 如下SpringBoot-2.2.x ...

  8. npm EPERM: operation not permitted

    缓存问题导致 需要删除npmrc文件. 强调:不是nodejs安装目录npm模块下的那个npmrc文件 而是在C:\Users\{账户}\下的.npmrc文件..

  9. Java向上保留两位小数

    setScale(2, BigDecimal.ROUND_UP) 例如:0.035 运算结果 为0.01

  10. PHP新特性

    1.太空船操作符<=> 2.变量类型限定 3.$a = $b??$c 4.常量数组,define($arr,['a','b']) 5.namespace批量导入 等等