#Leetcode# 951. Flip Equivalent Binary Trees
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:
- Each tree will have at most
100
nodes. - 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的更多相关文章
- 【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 ...
- 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 ...
- Leetcode951. Flip Equivalent Binary Trees翻转等价二叉树
我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树. 只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转等价于二叉树 Y. 编写一个判断 ...
- leetcode_951. Flip Equivalent Binary Trees_二叉树遍历
https://leetcode.com/problems/flip-equivalent-binary-trees/ 判断两棵二叉树是否等价:若两棵二叉树可以通过任意次的交换任意节点的左右子树变为相 ...
- [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 ...
- 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 ...
随机推荐
- php 批量去除项目文件bom头
<?php if (isset($_GET['dir'])) { //设置文件目录 $basedir = $_GET['dir']; } else { $basedir = '.'; } $au ...
- Mac进度条卡在100%
- Go正则处理
Go语言通过regexp标准包为正则表达式提供了官方支持 包中有三个函数判定是否匹配,匹配返回true,否则返回false,这三个函数只是输入源不同 func Match(pattern string ...
- PAT (Basic Level) Practice 1032 挖掘机技术哪家强
个人练习 为了用事实说明挖掘机技术到底哪家强,PAT 组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第 1 行给出不超过 10^5的正整数 N,即参赛人数 ...
- 12-oauth密码模式identity server4实现
1-服务端代码, 配置类,可加 RequireClientSecret=false, 这样调用端就不需要传入client_secret参数 using System.Collections; usin ...
- [原创]用python实现系统状态监测和故障转移
#coding: utf-8 import socket import select import time import os import threading def ser(): s = soc ...
- Java设计模式(17)——行为模式之观察者模式(Observer)
一.概述 概念 UML简图 我们根据一个示例得类图来分析角色 角色 抽象主题:保存观察者聚集(集合),管理(增删)观察者 抽象观察者:定义具体观察者的抽象接口,在得到主题通知后更新自己 具体主题:将有 ...
- 20145202马超《网络对抗》Exp9*_* Web安全基础实践
本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 1.实验后回答问题 (1)SQL注入攻击原理,如何防御 感觉上次做的也是sql注入,就是故意的非法输入(输入的是一些指令)让 ...
- PC平台逆向破解实验报告
PC平台逆向破解实验报告 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另 ...
- BZOJ4300_绝世好题_KEY
题目传送门 刚开始是看到这道题目还以为是序列连续的. 当然了,序列可以不连续. 设f[i]表示到第i位时的序列的最长长度. 取cnt=Max f[j]+1,然后转移回去使f[j]=cnt. 这是为了让 ...