作者: 负雪明烛
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:

  1. Each tree will have at most 100 nodes.
  2. 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)的更多相关文章

  1. #Leetcode# 951. Flip Equivalent Binary Trees

    https://leetcode.com/problems/flip-equivalent-binary-trees/ For a binary tree T, we can define a fli ...

  2. 【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 ...

  3. 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 ...

  4. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

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

  5. 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 ...

  6. [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 ...

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

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

  8. 【LeetCode】894. All Possible Full Binary Trees 解题报告(Python & C++)

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

  9. 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)

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

随机推荐

  1. python8 标准模块和第三方模块

  2. 全基因组选择育种(GS)简介

    全基因组选择(Genomic selection, GS)是一种利用覆盖全基因组的高密度标记进行选择育种的新方法,可通过早期选择缩短世代间隔,提高育种值(Genomic Estimated Breed ...

  3. 02 eclipse中配置Web项目(含eclipse基本配置和Tomcat的配置)

    eclipse搭建web项目 一.Eclipse基本配置 找到首选项: (一)配置编码 (二)配置字体 (三)配置jdk (四)配置Tomcat 二.Tomcat配置 三.切换视图,检查Tomcat ...

  4. CORS 如果需要指定多个域名怎么办

    CORS 通过控制 Access-Control-Allow-Origin 控制哪些域名可以共享资源,取值如下 Access-Control-Allow-Origin: <origin> ...

  5. 【STM32】晶振,主时钟,外设频率介绍

    首先,我用的是STM32F407,下方所有图片都是出自这芯片的文档,如果型号和我不同,需要找到对应的芯片说明文档,也许会有出入 先看一张时钟图 这里会着重说明高速的部分,低速(不管内部还是外部)只给R ...

  6. Android Https相关完全解析

    转载: 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/48129405: 本文出自:[张鸿洋的博客] 一.概述 其实这篇文章理论 ...

  7. 解决springboot序列化 json数据到前端中文乱码问题

    前言 关于springboot乱码的问题,之前有文章已经介绍过了,这一篇算是作为补充,重点解决对象在序列化过程中出现的中文乱码的问题,以及后台报500的错误. 问题描述 spring Boot 中文返 ...

  8. 【Linux】【Shell】【Basic】数组

    1. 数组:         变量:存储单个元素的内存空间:         数组:存储多个元素的连续的内存空间:             数组名:整个数组只有一个名字:             数组 ...

  9. 基于阿里云 ecs 使用 docker 方式部署 showDoc

    官网文档:https://www.showdoc.cc/help?page_id=65610 (建议先看下这个) 首先说明一下,我 ecs 镜像是 CentOS 7.6 64位 1. 首先在 服务器上 ...

  10. html如何让input number类型的标签不产生上下加减的按钮(转)

    添加css代码: <style> input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit- ...