【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 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的更多相关文章
- 【LeetCode】617. Merge Two Binary Trees 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【Leetcode_easy】617. Merge Two Binary Trees
problem 617. Merge Two Binary Trees 参考 1. Leetcode_easy_617. Merge Two Binary Trees; 完
- 【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【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 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 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 ...
- leetcode第一天-merge two binary trees
有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...
- [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 ...
- 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 ...
随机推荐
- 修改Eclipse启动时的选择工作空间
对于eclipse的默认的工作空间,如果不需要正常切换workspace的用户很方便,打开eclipse便自动进入默认的工作空间.而如果用户经常在多个workspace之间切换的话,启动eclipse ...
- vue+大文件断点续传
根据部门的业务需求,需要在网络状态不良的情况下上传很大的文件(1G+).其中会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长,请求超时:3,传输中断,必须重新上传导致前功尽弃.解 ...
- 用Excel如何将文本转换为数字的七种方法
用Excel如何将文本转换为数字的七种方法 当下,很多工作都会用到Excel,下面本文分步介绍了如何将包含文本的Excel单元格转换为包含数字的单元格. 概述: 当导入在另一程序(如 dBASE 或 ...
- React Native登录注册页面实现空白处收起键盘
其实很简单,直接使用ScrollView作为父视图即可.有木有很神奇啊,以前都还不知道呢.....
- laravel中的管道设计模式
转自 http://laravelacademy.org/post/3088.html
- 每次开机后需要重新连接wifi才能上网
这几天打开电脑后,每次都要重新连接wifi才能上网, 网上查到如下解决方法: 打开网络和共享中心->右键无线网络->配置->电源管理->允许计算机关闭此设备以节约电源(勾选去掉 ...
- 使用xdebug调试程序后程序很慢的原因
有一个原因就是开启调试的会话没有正确的关闭,即PhpStorm这边关闭了而没有通知服务端xdebug关闭,导致服务器资源被耗尽,这时只有重启服务端的服务才可以. 所以必须保证每一个调试会话被正确关闭. ...
- github pages + hexo 搭建 blog 遇到的问题
一. ERROR Deployer not found: git $ hexo d ERROR Deployer not found: git npm install --save hexo-depl ...
- java中类加载的全过程及内存图分析
类加载机制: jvm把class文件加载到内存,并对数据进行校验.解析和初始化,最终形成jvm可以直接使用的java类型的过程. (1)加载 将class文件字节码内容加载到内存中,并将这些静态数据转 ...
- Qt全局坐标和相对坐标
QMouseEvent中两类坐标系统,一类是窗口坐标,一类是显示器坐标. QPoint QMouseEvent::pos() 返回相对这个widget(重载了QMouseEvent的widget)的位 ...