【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 ...
随机推荐
- Linux -- 进程或线程独占CPU
如果想让特定进程或线程独占某一或某些CPU,我们需要做三件事. 一,隔离CPU,避免其它线程run在被隔离的CPU上. 二,绑定所有的interrupts到非隔离的CPU上,避免被隔离的CPU收到in ...
- 利用redis 分布式锁 解决集群环境下多次定时任务执行
定时任务: @Scheduled(cron= "0 39 3 * * *") public void getAllUnSignData(){ //检查任务锁,若其它节点的相同定时任 ...
- html+大文件上传
前言:因自己负责的项目(jetty内嵌启动的SpringMvc)中需要实现文件上传,而自己对java文件上传这一块未接触过,且对 Http 协议较模糊,故这次采用渐进的方式来学习文件上传的原理与实践. ...
- 启动mongodb报错,无法连接mongodb
报错原因如下: MongoDB shell version v3.4.2 connecting to: mongodb://127.0.0.1:27017 --01T12:: W NETWORK [t ...
- 【ARTS】01_25_左耳听风-201900429~20190505
ARTS: Algrothm: leetcode算法题目 Review: 阅读并且点评一篇英文技术文章 Tip/Techni: 学习一个技术技巧 Share: 分享一篇有观点和思考的技术文章 Algo ...
- 《剑指offer》树专题 (牛客10.25)
考察的知识点主要在于树的数据结构(BST,AVL).遍历方式(前序,中序,后序,层次).遍历算法(DFS,BFS,回溯)以及遍历时借助的数据结构如队列和栈.由于树本身就是一个递归定义的结构,所以在递归 ...
- YUV RGB 格式转换
第一个公式是RGB转YUV(范围0-255)时用的,第二个公式是用在YUV转换RGB(范围0-255)时用的.1. Y = ; U = -; V = ; 黑色:Y=16 ,U= V =128 红色:Y ...
- 日期控件传到后台异常。日期数据格式是 Date 还是 String?
问题:日期控件的时间,传到Controller层直接异常. 前台日期格式:YYYY/MM/DD,后台Java定义的时间类型:Date. 解决: 方法一:原因是Controller层的参数类型定义为 D ...
- 飞腾1500A 上面银河麒麟操作系统 进行远程以及添加用户的方法 linux xrdp
1. 安装远程用的软件: sudo apt-get install xrdp vnc4server xbase-clients systemctl enable xrdp systemctl star ...
- css 颜色自动变化 炫彩
.layui-icon-login-qq:hover{ color:rgb(0, 156, 255); transition: 0.5s; animation:change 10s linear 0s ...