Leetcode951. Flip Equivalent Binary Trees翻转等价二叉树
我们可以为二叉树 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.

提示:
- 每棵树最多有 100 个节点。
- 每棵树中的每个值都是唯一的、在 [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翻转等价二叉树的更多相关文章
- [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 ...
- 【LeetCode】951. Flip Equivalent Binary Trees 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 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 ...
- #Leetcode# 951. Flip Equivalent Binary Trees
https://leetcode.com/problems/flip-equivalent-binary-trees/ For a binary tree T, we can define a fli ...
- 【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/ 判断两棵二叉树是否等价:若两棵二叉树可以通过任意次的交换任意节点的左右子树变为相 ...
- LeetCode951-翻转等价二叉树
问题:翻转等价二叉树 我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树. 只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转等价于二叉 ...
- Invert a binary tree 翻转一棵二叉树
Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4 / \ 2 7 / \ / \ 1 3 6 9翻转后: 4 / \ 7 ...
- [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 ...
随机推荐
- buff/cache内存占用过多
通过free -m 查看到 buff/cache的值比较大,导致可使用的内存有120M左右了 通过下面的命令,清除缓存 echo 1 > /proc/sys/vm/drop_caches ech ...
- react 组件的生命周期 超简版
组件从被创建到被销毁的过程称为组件的 生命周期: 通常,组件的生命周期可以被分为三个阶段:挂载阶段.更新阶段.卸载阶段: 一.挂载阶段 这个阶段组件被创建,执行初始化,并被挂载到DOM中,完成组件的第 ...
- 2017/8/4 SCJP学习
2 Object Orientation . . . . . . . . . . . . . . . . . . . . . . . . . 85 Encapsulation (Exam Object ...
- Pycharm2019.1.3安装程序以及教程
链接:https://pan.baidu.com/s/1TF--EyCUQgmPeXFaCMJm8w 提取码:5vme
- P1030 求先序排列 /// 二叉树的遍历
题目大意: 给一棵树的中序排列 后序排列,求这棵树的先序排列 https://www.luogu.org/problemnew/show/P1030 二叉树的四种遍历解说 几种遍历的递归实现 后序排列 ...
- OdDbAttribute和OdDbAttributeDefinition是什么关系
OdDbAttributeDefinition是定义,比如说是英文,是一个占位符: OdDbAttribute就是具体的东西,比如是abc
- scull 中的设备注册
在内部, scull 使用一个 struct scull_dev 类型的结构表示每个设备. 这个结构定义为: struct scull_dev { struct scull_qset *data; ...
- Vue项目在Docker的自动化部署
操作系统:CentOS 部署环境:Docker CI/CD工具:Jenkins 1.环境配置 安装Jenkins:Centos安装Jenkins 安装Docker:Centos安装Git.DotNet ...
- 【学术篇】luogu1558&&poj2777 色板游戏
题目の传送门: luogu:https://www.luogu.org/problem/show?pid=1558 poj:http://poj.org/problem?id=2777 题目大意:给有 ...
- barrel shifter, logarthmic shifter and funnel shifter
1,shifter小集合 (1) simple shift 左移或右移补0 (2) arthmetic shift 左移补0,右移补符号位 (3) barrel shifter 桶型,顾名思义,应该头 ...