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:

  1. Each tree will have at most 100 nodes.
  2. Each value in each tree will be a unique integer in the range [0, 99].

leetcode_951. 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. 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 ...

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

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

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

  5. #Leetcode# 951. Flip Equivalent Binary Trees

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

  6. Leetcode951. Flip Equivalent Binary Trees翻转等价二叉树

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

  7. 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历

    [二叉树遍历模版]前序遍历     1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...

  8. poj2255 (二叉树遍历)

    poj2255 二叉树遍历 Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descripti ...

  9. D - 二叉树遍历(推荐)

    二叉树遍历问题 Description   Tree Recovery Little Valentine liked playing with binary trees very much. Her ...

随机推荐

  1. IE、W3C两种CSS盒子模型

    利用CSS来布局页面布局DIV有点逻辑性!重点理解盒子模型,标准流和非标准流的区别,还有定位原理!把这3个攻破了,就非常简单了!多实践多参考!最后就是兼容问题了,在实践中自然就有经验了!这些兼容技巧都 ...

  2. 地图上显示X,Y 坐标代码

    事件数据 所有的鼠标事件都使用MouseButtonEventArgs和MouseEventArgs作为事件数据,通过这两个参数可以获取相关事件数据,使用GetPosition方法或者Source.H ...

  3. C# ListView 列宽调整 刷新

    /*********************************************************************** * C# ListView 列宽调整 刷新 * 说明: ...

  4. spring-boot快速搭建解析

    创建方式: 1.在File菜单里面选择 New > Project,然后选择Spring Initializr: 2.使用maven直接构建,添加依赖. 1 2 3 4 pom.xml:Mave ...

  5. html/html5中的download属性

    兼容性不是很好, 只是了解一下: 主要表现在跨域策略的处理上,Chrome浏览器和FireFox浏览器: 如果需要下载的资源是跨域的,包括跨子域,在Chrome浏览器下,使用download属性是可以 ...

  6. glance镜像服务

    一.glance介绍: 因为云平台是提供Iass层的基础设施服务,我们拿到的是一台虚拟机,那么要用虚拟机的话必须有底层的镜像做支撑,所以说镜像的话也有一个服务来管理.但是我们云平台用的镜像不是装操作系 ...

  7. Luogu P1280 Niko的任务【线性dp】By cellur925

    Nikonikoni~~ 题目传送门 这是当时学长讲dp的第一道例题,我还上去献了个丑,然鹅学长讲的方法我似董非董(??? 我当时说的怎么设计这道题的状态,但是好像说的是二维,本题数据范围均在1000 ...

  8. javascript 冒泡与捕获的原理及操作实例

    所谓的javascript冒泡与捕获不是数据结构中的冒泡算法,而是javascript针对dom事件处理的先后顺序,所谓的先后顺序是指针对父标签与其嵌套子标签,如果父标签与嵌套子标签均有相同的事件时, ...

  9. Drawable新属性

    mSelectEndorseReasonTv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.toup, 0);新属性替换: Draw ...

  10. 在Python解释器运行程序

    在解释器中运行  ***.py文件的方法:使用import添加模块 ***.py,然后调用 ***.py中的函数 例:在zoo.py中定义hours函数 运行方法: >>> impo ...