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 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]
.
A、B两颗二叉树相等当且仅当rootA->data == rootB->data,且A、B的左右子树相等或者左右互换相等
/**
* 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 ;
if ((!root1&&root2) || (root1 && !root2))
return ;
if (root1&&root2)
{
if (root1->val == root2->val)
{
if (flipEquiv(root1->left, root2->left))
return flipEquiv(root1->right, root2->right);
else if (flipEquiv(root1->left, root2->right))
return flipEquiv(root1->right, root2->left);
}
}
return ;
}
};
113th LeetCode Weekly Contest 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 ...
- #Leetcode# 951. Flip Equivalent Binary Trees
https://leetcode.com/problems/flip-equivalent-binary-trees/ For a binary tree T, we can define a fli ...
- [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 ...
- 113th LeetCode Weekly Contest Reveal Cards In Increasing Order
In a deck of cards, every card has a unique integer. You can order the deck in any order you want. ...
- 113th LeetCode Weekly Contest Largest Time for Given Digits
Given an array of 4 digits, return the largest 24 hour time that can be made. The smallest 24 hour t ...
- 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 weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
随机推荐
- Windows cmd 快捷操作
复制当前文件夹下符合条件的文件名字到文件 dir /B *-gd.dll > debug /B是只显示文件名,不包含所在路径 复制当前文件夹下文件到另外的地方 xcopy /s/d *-gd.d ...
- 为什么rand和srand总是同时出现?
如果没有srand,那么rand在我电脑上运行每次返回的随机数是一样的.如果如果先调用srand,而且srand的参数不一样,那么最后产生的随机数就会不一样?那怎么然srand的参数是不一样的呢? 是 ...
- Entity Framework Tutorial Basics(40):Validate Entity
Validate Entity You can write custom server side validation for any entity. To accomplish this, over ...
- .Net 数据库(SqlServer2008)的备份、还原
//备份代码private void Backup() { SqlConnection sqlConn = new SqlConnection(strConn); strFileName = &quo ...
- POJ3233 Matrix Power Series(矩阵快速幂+分治)
Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. ...
- 合成(Composite)模式
一. 合成(Composite)模式 合成模式有时又叫做部分-整体模式(Part-Whole).合成模式将对象组织到树结构中,可以用来描述整体与部分的关系. 合成模式可以使客户端将单纯元素与复合元素同 ...
- Snapshot--使用脚本创建快照
USE master; SET NOCOUNT ON; GO ); --数据库名 );--快照名 );--保存路径 SET @dbname='DB1'; SET @snapname='DB1_SNAP ...
- 表单使用clone方法后, 原有select无法生效
textarea和select的值clone的时候会丢掉,在clone的时候将val再重新赋值一下,如果知道这个了就加单了 测试发现,textarea和select的jquery的clone方法有 ...
- IEnumerable与IQueryable区别
最近在使用MongoDB的时候,发现查询很慢,一个根据Id查询的语句竟然用了50秒,debug了一下,没发现什么大问题,但是另一个查询的语句只用了2秒,对比了一下,发现50s的那个语句使用的IEnum ...
- Glib学习笔记(四)
你将学到什么 使用GObject模拟实现接口 使用接口 首先按照学习笔记(一)定义一个普通的GObject类 使用G_DEFINE_TYPE_WITH_CODE和G_IMPLEMENT_INTERFA ...