我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树。

只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转等价于二叉树 Y。

编写一个判断两个二叉树是否是翻转等价的函数。这些树由根节点 root1 和 root2 给出。

示例:

输入: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] 输出:true 解释:We flipped at nodes with values 1, 3, and 5.

提示:

  1. 每棵树最多有 100 个节点。
  2. 每棵树中的每个值都是唯一的、在 [0, 99] 范围内的整数。

递归求解,交换参数节点就相当于交换了。

class Solution {
public:
bool flipEquiv(TreeNode* root1, TreeNode* root2)
{
if(root1 == NULL && root2 == NULL)
return true;
else if(root1 == NULL && root2 != NULL || root1 != NULL && root2 == NULL)
return false;
else if(root1 ->val == root2 ->val)
{
return
(flipEquiv(root1 ->left, root2 ->left) && flipEquiv(root1 ->right, root2 ->right)) ||
(flipEquiv(root1 ->right, root2 ->left) && flipEquiv(root1 ->left, root2 ->right));
}
else
{
return false;
}
}
};

Leetcode951. Flip Equivalent Binary Trees翻转等价二叉树的更多相关文章

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

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

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

  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. #Leetcode# 951. Flip Equivalent Binary Trees

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

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

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

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

  7. LeetCode951-翻转等价二叉树

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

  8. Invert a binary tree 翻转一棵二叉树

    Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4  / \   2    7  / \   / \ 1  3 6  9翻转后: 4     /    \    7 ...

  9. [LeetCode] 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 ...

随机推荐

  1. leetcode-216-组合总和③

    题目描述: 方法一:回溯 class Solution: def combinationSum3(self, k: int, n: int) -> List[List[int]]: res = ...

  2. 为什么串行传输时总是LSB在前?

    https://superuser.com/questions/1104212/why-do-serial-ports-send-data-least-significant-bit-first 其实 ...

  3. SpringBoot生产/开发/测试多环境的选择

    多环境选择 一般一套程序会被运行在多部不同的环境中,比如开发.测试.生产环境,每个环境的数据库地址,服务器端口这些都不经相同,若因为环境的变动而去改变配置的的参数,明显是不合理且易造成错误的 对于不同 ...

  4. VS2010-MFC(MFC常用类:CTime类和CTimeSpan类)

    转自:http://www.jizhuomi.com/software/230.html 上一节讲了MFC常用类CString类的用法,本节继续讲另外两个MFC常用类-日期和时间类CTime类和CTi ...

  5. 计算图像数据集的RGB均值

    最近在跑代码的时候,需要用到RGB三个通道上的均值,如下图所示: 写了一个程序,如下: import os import cv2 import random import numpy as np #数 ...

  6. 读书笔记 | 敏捷编码&敏捷调试

    这周的个人项目让我感受到自己在编程方面的不足和缺陷,所以选择了<高效程序员的45个习惯>中的敏捷开发和敏捷调试两个章节进行阅读. 以下将对敏捷开发和敏捷调试展开详述. [敏捷开发] 注释 ...

  7. 创建 linuxrc 文件

    创建 linuxrc,加入如下内容: [arm@localhost my_rootfs]#vi linuxrc #!/bin/sh #挂载/etc 为 ramfs, 并从/mnt/etc 下拷贝文件到 ...

  8. printk函数打开和关闭消息

    在驱动开发的早期, printk 非常有助于调试和测试新代码. 当你正式发行驱动时, 换句 话说, 你应当去掉, 或者至少关闭, 这些打印语句. 不幸的是, 你很可能会发现, 就在你 认为你不再需要这 ...

  9. JS获取url参数,修改url参数

    function getURL(){ var args = {}; var query = location.search.substring(1); //获得了当前链接的中?号后的参数 var pa ...

  10. mongodb 3.2 yaml 配置详解及范例

    mongodb3.x版本后就是要yaml语法格式的配置文件,下面是yaml配置文件格式如下:官方yaml配置文件选项参考:https://docs.mongodb.org/manual/ ... #c ...