leetcode_951. Flip Equivalent Binary Trees_二叉树遍历
https://leetcode.com/problems/flip-equivalent-binary-trees/
判断两棵二叉树是否等价:若两棵二叉树可以通过任意次的交换任意节点的左右子树变为相同,则称两棵二叉树等价。
思路:遍历二叉树,判断所有的子树是否等价。
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x)
: val(x), left(NULL), right(NULL) {}
}; class Solution
{
public:
void exchangeSons(TreeNode* root)
{
TreeNode* tmp = root->left;
root->left = root->right;
root->right = tmp;
} int getNum(TreeNode* node)
{
if(node == NULL)
return -;
else
return node->val;
}
int compareSons(TreeNode* root1, TreeNode* root2)
{
TreeNode* left1 = root1->left;
TreeNode* right1 = root1->right;
TreeNode* left2 = root2->left;
TreeNode* right2 = root2->right;
int l1,l2,r1,r2;
l1 = getNum(left1);
l2 = getNum(left2);
r1 = getNum(right1);
r2 = getNum(right2);
if(l1 == l2 && r1 == r2)
return ;
else if(l1 == r2 && r1 == l2)
return ;
else
return ;
}
bool flipEquiv(TreeNode* root1, TreeNode* root2)
{
if(root1 == NULL && root2 == NULL)
return ;
else if(root1 == NULL)
return ;
else if(root2 == NULL)
return ;
int comres = compareSons(root1, root2);
if(comres == )
return ;
else if(comres == )
exchangeSons(root2);
bool leftEquiv = ,rightEquiv = ;
if(root1->left != NULL)
leftEquiv = flipEquiv(root1->left, root2->left);
if(root1->right != NULL)
rightEquiv = flipEquiv(root1->right, root2->right);
if(leftEquiv&&rightEquiv)
return ;
else
return ;
}
};
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]
.
leetcode_951. 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 ...
- 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
题目如下: 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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- #Leetcode# 951. Flip Equivalent Binary Trees
https://leetcode.com/problems/flip-equivalent-binary-trees/ For a binary tree T, we can define a fli ...
- Leetcode951. Flip Equivalent Binary Trees翻转等价二叉树
我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树. 只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转等价于二叉树 Y. 编写一个判断 ...
- 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历
[二叉树遍历模版]前序遍历 1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...
- poj2255 (二叉树遍历)
poj2255 二叉树遍历 Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descripti ...
- D - 二叉树遍历(推荐)
二叉树遍历问题 Description Tree Recovery Little Valentine liked playing with binary trees very much. Her ...
随机推荐
- 推荐一款很好用的调试js的eclipse插件
ie调试的话用 Companion.JS 一个插件 很好用的 不用配置,直接安装eclipse调试的话 可以用jsdt 可能需要配置下 网上有很多说明http://wokaours.blog.163. ...
- 性能-发挥ORACLE分区表
ORACLE分区表发挥性能 http://www.cnblogs.com/zwl715/p/3962837.html 1.1 分区表PARTITION table 在ORACLE里如果遇到特别大的表, ...
- PCB RabbitMQ的安装使用
随着公司加大力度信息化建设,PCB企业各种各样的系统软件越来越多,整个公司订单流状态监控变得越来越不可控,是时候需采用新的方式来收集各系统状态节点状态了,以下记录RabbitMQ安装使用: 一.Rab ...
- ThinkPHP3.2.3学习笔记3---视图
一.说明 每个模块的模板文件是独立的,为了对模板文件更加有效的管理,ThinkPHP对模板文件进行目录划分,默认的模板文件定义规则是:视图目录/[模板主题/]控制器名/操作名+模板后缀 默认的视图目录 ...
- 洛谷 P3708 koishi的数学题
找规律发现\( f[i]=f[i-1]+n-\sum_{i的因数和} \) 一A了深(sh)蓝(ui)题的我被找规律绿题卡死 记得开long long #include<iostream> ...
- NOIp 2017 奶酪 【并查集】 By cellur925
题目传送门 Orz去年考场上做这道题的我应该还在抱怨没学过空间几何,不一会太困了就开始打瞌睡,然后为了防止睡觉开始在devc++上写默写离骚(逃 思路:如果两个空洞相交,那么把他们并在一个集合里.最后 ...
- windows环境安装和使用curl与ES交互
一.下载安装 去官网下载对应版本的包,解压后打开CMD切换到对应目录(我的目录,E:\file\I386)下运行CURL.exe文件, 如果把该CURL.exe文件复制到C:\Windows\Syst ...
- C++中的显式构造函数
有如下一个简单的复数类: class ClxComplex { public: ClxComplex(double dReal = 0.0, double dImage = 0.0) { m_dRea ...
- 《Windows核心编程系列》二十谈谈DLL高级技术
本篇文章将介绍DLL显式链接的过程和模块基地址重定位及模块绑定的技术. 第一种将DLL映射到进程地址空间的方式是直接在源代码中引用DLL中所包含的函数或是变量,DLL在程序运行后由加载程序隐式的载入, ...
- 二进制流BinaryFormatter存储读取数据的细节测试
二进制流的使用很方便,为了更好的理解应用它,我创建简单对象开始测试它的增加特性和减少特性. [Serializable] class Data----------开始时候的存储对象 { public ...