https://leetcode.com/problems/flip-equivalent-binary-trees/

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

代码:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool flipEquiv(TreeNode* root1, TreeNode* root2) {
if(!root1 && !root2) return true;
if(!root1 || !root2) return false;
if(root1 -> val != root2 -> val) return false;
return flipEquiv(root1 -> left, root2 -> right) && flipEquiv(root1 -> right, root2 -> left) ||
flipEquiv(root1 -> left, root2 -> left) && flipEquiv(root1 -> right, root2 -> right);
}
};

  为什么要  这个判断

#Leetcode# 951. Flip Equivalent Binary Trees的更多相关文章

  1. 【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)

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

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

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

  5. Leetcode951. Flip Equivalent Binary Trees翻转等价二叉树

    我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树. 只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转等价于二叉树 Y. 编写一个判断 ...

  6. leetcode_951. Flip Equivalent Binary Trees_二叉树遍历

    https://leetcode.com/problems/flip-equivalent-binary-trees/ 判断两棵二叉树是否等价:若两棵二叉树可以通过任意次的交换任意节点的左右子树变为相 ...

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

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

  9. LeetCode 617. Merge Two Binary Trees合并二叉树 (C++)

    题目: Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...

随机推荐

  1. 分布式时间同步ntp安装

    直接执行:sudo yum install ntp或者sudo -y install ntp

  2. VSCode 配置 Python

    假设 Anaconda 和 VSCode 都安装好了. 安装插件 VSCode 自带的 python 高亮不是很好,这里我们用 One Dark Pro 插件, 以及安装 Python 插件. 设定 ...

  3. 【HDOJ-1081】To The Max(动态规划)

    To the Max Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...

  4. CORDIC算法(1):圆周旋转模式下计算三角函数和模值

    CORDIC(Coordinate Rotation Digital Computer)坐标旋转数字计算机,是数学与计算机技术交叉产生的一种机器算法,用于解决计算机的数学计算问题.发展到现在,CORD ...

  5. 【BZOJ2286】消耗战(虚树,动态规划)

    [BZOJ2286]消耗战(虚树,动态规划) 题面 BZOJ Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总 ...

  6. Sql Server Profiler使用

            在使用Entity Framework的过程当中,有时候需要看Entity Framework自动生成的Sql语句,在客户端可以使用跟踪的方法看到每次查询时的Sql语句,其实通过数据库 ...

  7. 这样的SQL居然能执行

    select /*! distinct   cities.id from cities  join countries on cities.id = countries.id limit 10 */;

  8. 一些窍门 drawable

    java.lang.Object       android.graphics.drawable.DrawableKnown Direct Subclasses   BitmapDrawable, C ...

  9. debian8+lnmp1.2一键安装+WordPress3.9

    下载并安装LNMP一键安装包 wget -c http://soft.vpser.net/lnmp/lnmp1.2-full.tar.gz && tar zxf lnmp1.2-ful ...

  10. Selenium(Python)生成Html测试报告

    由于Python3已经不支持HTMLTestRunner了, 无论是PyCharm还是pip都无法安装成功, 所以只能去 http://tungwaiyip.info/software/HTMLTes ...