Determine whether two given binary trees are identical assuming any number of ‘tweak’s are allowed. A tweak is defined as a swap of the children of one node in the tree.

Examples

5

/    \

3        8

/   \

1      4

and

5

/    \

8        3

/   \

1     4

the two binary trees are tweaked identical.

How is the binary tree represented?

We use the level order traversal sequence with a special symbol "#" denoting the null node.

For Example:

The sequence [1, 2, 3, #, #, 4] represents the following binary tree:

1

/   \

2     3

/

4

time: O(4 ^ log_2(n)) = O(n ^ 2), space: O(h)

/*** public class TreeNode {*   public int key;*   public TreeNode left;*   public TreeNode right;*   public TreeNode(int key) {*     this.key = key;*   }* }*/
public class Solution {public boolean isTweakedIdentical(TreeNode one, TreeNode two) {// Write your solution hereif(one == null && two == null) {return true;} else if(one == null || two == null) {return false;}else if(one.key != two.key) {return false;}return isTweakedIdentical(one.left, two.right) && isTweakedIdentical(one.right, two.left) || isTweakedIdentical(one.left, two.left) && isTweakedIdentical(one.right, two.right);}
}

转载于:https://www.cnblogs.com/fatttcat/p/10253373.html

Tweaked Identical Binary Trees - Medium相关推荐

  1. LeetCode之All Possible Full Binary Trees(Kotlin)

    问题: A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list o ...

  2. 17. Merge Two Binary Trees 融合二叉树

    [抄题]: Given two binary trees and imagine that when you put one of them to cover the other, some node ...

  3. LeetCode 617. Merge Two Binary Trees

    题目: Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...

  4. C#LeetCode刷题之#617-合并二叉树​​​​​​​​​​​​​​(Merge Two Binary Trees)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4096 访问. 给定两个二叉树,想象当你将它们中的一个覆盖到另一个 ...

  5. Given two binary trees, write a function to check if they areequal or not.

    Given two binary trees, write a function to check if they areequal or not. Two binary trees are cons ...

  6. (数据结构基础)Among the following threaded binary trees (the threads are represented by dotted curves),……

    当年学这些的时候真的是苦于没有人讲,现在复习考研,我会见到这种题就写下来,学弟学妹们欢迎点个关注,最近也在创业想实习的可以找我联系:没看懂的话是我表述有问题,欢迎指出和私戳. Among the fo ...

  7. Rosalind第88题:Counting Rooted Binary Trees

    Problem As in the case of unrooted trees, say that we have a fixed collection of  taxa labeling the  ...

  8. 617.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 ...

  9. 3/100. Merge Two Binary Trees

    将两个二叉树相同位置的数值相加,相加的方法一样,则使用递归法挨个相加即可. # Definition for a binary tree node. # class TreeNode: # def _ ...

最新文章

  1. 【java】兴唐第十八节课
  2. JAVA     面向对象
  3. Java实现数据库表结构导出到Excel
  4. linux下的代码比较工具下载,linux下的代码工具比较
  5. 如何设计一门语言(三)——什么是坑(面向对象和异常处理)
  6. 腾讯正式加入OCP阵营,拥抱全球开源生态圈
  7. python类的使用(类定义,构造器,类属性,方法)
  8. Percona Server for MySQL 5.5.30-30.2
  9. openshift_OpenShift Origin中的Kubernetes Spark运算符(第1部分)
  10. 2019 第八/九周/十周 开发笔记
  11. Android第二十课 解决Logcat无法输出调试信息
  12. visual studio 2015安装教程
  13. javaSE开发智能问答机器人项目
  14. 「网络安全」将会是下一个风口?这个“下饭神剧”值得一看!
  15. Linux搜索日志关键字的2种方法
  16. Codeforces 1144 D
  17. word中统一修改mathtype公式和大小对应
  18. 大数据就业:学完大数据怎样就业
  19. qemu 加载ubuntu
  20. android游戏手柄,没手柄也不怕 键盘玩Android游戏攻略

热门文章

  1. quartusii开发过程中路径不能出现空格或中文
  2. 使用PHP Excel类读取和生成excel文件
  3. 1.7nginx用户认证
  4. .Net使用Redis详解之ServiceStack.Redis(七)
  5. 【配置映射】—Entity Framework实例详解
  6. VC编写自己构造http协议数据的post上传图片类(MFC环境 带编码转换)(转)
  7. IP多播技术介绍(二)
  8. 职场经典小故事-II
  9. Java-函数式编程(二)Lambda表达式
  10. [转]python的requests发送/上传多个文件