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
100nodes. - 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 ...
随机推荐
- Deep Learning 29: caffe入门学习
1.跑教程:深度学习(六)caffe入门学习,上面有比较好的注释 .prototxt文件:网络结构文件 solver.prototxt:网络求解文件 net: "examples/mnist ...
- HDU4612 Warm up —— 边双联通分量 + 重边 + 缩点 + 树上最长路
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4612 Warm up Time Limit: 10000/5000 MS (Java/Ot ...
- YTU 2430: C语言习题 链表建立,插入,删除,输出
2430: C语言习题 链表建立,插入,删除,输出 时间限制: 1 Sec 内存限制: 128 MB 提交: 576 解决: 280 题目描述 编写一个函数creatlink,用来建立一个动态链表 ...
- YTU 2562: 黄金螺旋
2562: 黄金螺旋 时间限制: 1 Sec 内存限制: 128 MB 提交: 832 解决: 427 题目描述 黄金螺旋是根据斐波那契数列画出来的螺旋曲线,自然界中存在许多斐波那契螺旋线的图案, ...
- 函数,#include <>和#include " "区别
l 4..函数定义到使用分三步:1.声明.2.定义函数.3.调用函数. 2.函数的定义格式:返回值 函数名(形参列表){ 函数体; return } 命名规则:1.只能由字母.数字.下划线或者美元符号 ...
- UI:通讯录实现
通讯录实现草图: 代码: #pragma mark (.h文件)-------------------------------------------------------------------- ...
- nginx下配置虚拟主机
linux 虚拟机下配置虚拟主机 nginx.conf 文件不动, 在 conf.d 或者 conf 目录下 新建项目.conf server { listen 80; server_name loc ...
- Canvas 入门案例
五. Canvas 入门案例 1. canvas 圆形绘制 <!DOCTYPE html> <html lang="en"> <head> ...
- SpringBoot项目docker化
前言 有很多种方案构建Docker镜像,包括Dockerfile构建.maven插件构建,这里我使用了最简单的Dockerfile构建的. 一.安装Docker 我的虚拟机系统是CentOS7,需要是 ...
- mqtt遇到的问题锦集
1.无效客户机标识 (2) Connect指令中的KeepAlive有效范围[60秒,300秒],否则会拒绝连接. 2.消息回调出现频繁的断开连接 待解决 3.长时间消息回调出现 已断开连接 (321 ...