https://leetcode.com/problems/flip-equivalent-binary-trees/

判断两棵二叉树是否等价:若两棵二叉树可以通过任意次的交换任意节点的左右子树变为相同,则称两棵二叉树等价。

思路:遍历二叉树,判断所有的子树是否等价。

  1. struct TreeNode
  2. {
  3. int val;
  4. TreeNode *left;
  5. TreeNode *right;
  6. TreeNode(int x)
  7. : val(x), left(NULL), right(NULL) {}
  8. };
  9.  
  10. class Solution
  11. {
  12. public:
  13. void exchangeSons(TreeNode* root)
  14. {
  15. TreeNode* tmp = root->left;
  16. root->left = root->right;
  17. root->right = tmp;
  18. }
  19.  
  20. int getNum(TreeNode* node)
  21. {
  22. if(node == NULL)
  23. return -;
  24. else
  25. return node->val;
  26. }
  27. int compareSons(TreeNode* root1, TreeNode* root2)
  28. {
  29. TreeNode* left1 = root1->left;
  30. TreeNode* right1 = root1->right;
  31. TreeNode* left2 = root2->left;
  32. TreeNode* right2 = root2->right;
  33. int l1,l2,r1,r2;
  34. l1 = getNum(left1);
  35. l2 = getNum(left2);
  36. r1 = getNum(right1);
  37. r2 = getNum(right2);
  38. if(l1 == l2 && r1 == r2)
  39. return ;
  40. else if(l1 == r2 && r1 == l2)
  41. return ;
  42. else
  43. return ;
  44. }
  45. bool flipEquiv(TreeNode* root1, TreeNode* root2)
  46. {
  47. if(root1 == NULL && root2 == NULL)
  48. return ;
  49. else if(root1 == NULL)
  50. return ;
  51. else if(root2 == NULL)
  52. return ;
  53. int comres = compareSons(root1, root2);
  54. if(comres == )
  55. return ;
  56. else if(comres == )
  57. exchangeSons(root2);
  58. bool leftEquiv = ,rightEquiv = ;
  59. if(root1->left != NULL)
  60. leftEquiv = flipEquiv(root1->left, root2->left);
  61. if(root1->right != NULL)
  62. rightEquiv = flipEquiv(root1->right, root2->right);
  63. if(leftEquiv&&rightEquiv)
  64. return ;
  65. else
  66. return ;
  67. }
  68. };

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:

  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]
  2. Output: true
  3. 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. ABAP 读取服务器CSV文件到内表

    DATA: BEGIN OF gs_data , cola TYPE string, colb TYPE string, ... END OF gs_data, gt_data LIKE TABLE ...

  2. (21) java web的struts2框架的使用-Action实现的三种方式

    上一篇介绍了struts使用的四个步骤. 其中在开发action的时候,可以有三种实现方式: 1,写一个类,继承与ActionSupport 2,写一个类,实现Action接口 3,写一个类,实现业务 ...

  3. 关于warning: Clock skew detected. Your build may be incomplete. 的解决方法【转】

    本文转载自:http://blog.csdn.net/jeesenzhang/article/details/40300127 今天发现电脑的系统时间不正确,因此将时钟进行了修改,回头编译Linux ...

  4. ubuntu php5.6源码安装

    本系列的lnmp的大框架基本上是按照http://www.linuxzen.com/lnmphuan-jing-da-jian-wan-quan-shou-ce-si-lnmpda-jian-yuan ...

  5. What's the difference between HEAD, working tree and index, in Git?

    What's the difference between HEAD, working tree and index, in Git?

  6. excel+requests管理测试用例接口自动化框架

    背景: 某项目有多个接口,之前使用的unittest框架来管理测试用例,将每个接口的用例封装成一个py文件,接口有数据或者字段变动后,需要去每个py文件中找出变动的接口测试用例,维护起来不方便,为了便 ...

  7. 从0开始学习Hadoop(1) 环境准备 Win7环境+VirtureBox+Ubuntu

    虚拟机:VirtureBox 3.18 下载地址: https://www.virtualbox.org/ 操作系统:Ubuntu  版本:ubuntu-15.04-desktop-amd64.iso ...

  8. 04_显示意图打开activity

    实际上用显式意图打开一个activity就很简单了.只需要指定你要打开的这个activity的class就可以了. 需要注意一点的是创建了一个ThirdActivity必须要在清单文件里面声明.如果没 ...

  9. 一步一步学Vue(六)

    本篇继续介绍vue-router,我们需要要完成这样个demo:<分页显示文章列表>:这里我们以博客园首页列表为例简化处理: 按照上图框选所示,简单分为蓝色部分文章组件(ArticleIt ...

  10. 【Aizu - 0005 】GCD and LCM

    GCD and LCM Descriptions: Write a program which computes the greatest common divisor (GCD) and the l ...