【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/flip-equivalent-binary-trees/description/
题目描述
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.
A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations.
Write a function that determines whether two binary trees are flip equivalent. The trees are given by root nodes root1 and root2.
Example 1:
Input: root1 = [1,2,3,4,5,6,null,null,null,7,8], root2 = [1,3,2,null,6,4,5,null,null,null,null,8,7]
Output: true
Explanation: We flipped at nodes with values 1, 3, and 5.

Note:
- Each tree will have at most 100 nodes.
- Each value in each tree will be a unique integer in the range [0, 99].
题目大意
两棵二叉树,判断他们对某些节点的左右子树进行互换之后,能不能相等。
解题方法
递归
这个题优美的递归,让我们不禁感叹编程真的简化了这个世界。
题目中我们不确定翻转了哪些节点,首先我们可以知道如果两个树都是空树,那么可以互相得到。如果两个数有一个是空,另一个不空,那么一定不能互相得到。如果两个树的节点的值不等,也不能互相得到。
重点来了:我们现在已经确定了两个树都不空,企鹅值相等,如何确定它们的子树们是否翻转相等呢?首先,我们来回顾一下flipEquiv函数的含义:判断两个树在进行翻转/不进行翻转的情况下,能不能相等。所以,我们不确定两个子树的状况,只需要对两个子树进行翻转/不翻转两种状态判断即可。如果进行翻转,那么root1的左子树可以通过root2的右子树得到,同时root1的左子树通过root2的右子树得到;如果不进行翻转,那么root1和root2的对应左右子树通过操作应该也能互相得到。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def flipEquiv(self, root1, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: bool
"""
if not root1 and not root2: return True
if not root1 and root2: return False
if root1 and not root2: return False
if root1.val != root2.val: return False
return (self.flipEquiv(root1.left, root2.right) and self.flipEquiv(root1.right, root2.left)) or (self.flipEquiv(root1.left, root2.left) and self.flipEquiv(root1.right, root2.right))
日期
2018 年 12 月 2 日 —— 又到了周日
【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)的更多相关文章
- #Leetcode# 951. Flip Equivalent Binary Trees
https://leetcode.com/problems/flip-equivalent-binary-trees/ For a binary tree T, we can define a fli ...
- 【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 617 Merge Two Binary Trees 解题报告
题目要求 Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 113th LeetCode Weekly Contest Flip Equivalent Binary Trees
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left a ...
- [Swift]LeetCode951. 翻转等价二叉树 | Flip Equivalent Binary Trees
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left a ...
- 【LeetCode】617. Merge Two Binary Trees 解题报告
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】894. All Possible Full Binary Trees 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
随机推荐
- [Linux] 非root安装Lefse软件及其数据分析
说明 Lefse软件是宏组学物种研究常用软件,一般大家用在线版本即可.但要搭建在Linux集群环境中有点烦,记录一下折腾过程. 安装 这个软件是python2写的,因此假设我已经安装好了较高版本的py ...
- shell批量创建用户
#!/bin/bash cat << EOF ************************************************************ 批量添加用户并随机生 ...
- 关于蓝牙Mesh您必须知道的七件事
蓝牙技术联盟于7月19日正式宣布,蓝牙(Bluetooth)技术开始全面支持Mesh网状网络.全新的Mesh功能提供设备间多对多传输,并特别提高构建大范围网络覆盖的通信能力,适用于楼宇自动化.无线传感 ...
- kubernetes部署 flannel网络组件
创建 flannel 证书和私钥flannel 从 etcd 集群存取网段分配信息,而 etcd 集群启用了双向 x509 证书认证,所以需要为 flanneld 生成证书和私钥. cat > ...
- CMakeLists.txt添加多个源代码
coos2d-x 3.17.2 C++工程,安卓编译使用CMake,按照模板给的写法,只能一个一个源文件添加:如果需要添加大量的C++源代码,这种方式肯定不可取:原来的写法: 1 list(APPEN ...
- 报错:Unsupported field: HourOfDay
报错:Unsupported field: HourOfDay 这个错误就比较搞笑也比较低级了. 代码如下 LocalDate now = LocalDate.now(); String year = ...
- Spark相关知识点(一)
spark工作机制,哪些角色,作用. spark yarn模式下的cluster模式和client模式有什么区别.
- 大数据学习day31------spark11-------1. Redis的安装和启动,2 redis客户端 3.Redis的数据类型 4. kafka(安装和常用命令)5.kafka java客户端
1. Redis Redis是目前一个非常优秀的key-value存储系统(内存的NoSQL数据库).和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list ...
- c#中实现串口通信的几种方法
c#中实现串口通信的几种方法 通常,在C#中实现串口通信,我们有四种方法: 第一:通过MSCOMM控件这是最简单的,最方便的方法.可功能上很难做到控制自如,同时这个控件并不是系统本身所带,所以还得注册 ...
- jQuery - 按回车键触发跳转
键盘事件有三种: keyup:按键按下去,抬上来后,事件才生效 (推荐) keydown:按键按下去就生效 keypress:与 keydown 事件类似,当按钮被按下时,会发生该事件,与 keydo ...