Tweaked Identical Binary Trees - Medium
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相关推荐
- 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 ...
- 17. Merge Two Binary Trees 融合二叉树
[抄题]: Given two binary trees and imagine that when you put one of them to cover the other, some node ...
- 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 ...
- C#LeetCode刷题之#617-合并二叉树(Merge Two Binary Trees)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4096 访问. 给定两个二叉树,想象当你将它们中的一个覆盖到另一个 ...
- 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 ...
- (数据结构基础)Among the following threaded binary trees (the threads are represented by dotted curves),……
当年学这些的时候真的是苦于没有人讲,现在复习考研,我会见到这种题就写下来,学弟学妹们欢迎点个关注,最近也在创业想实习的可以找我联系:没看懂的话是我表述有问题,欢迎指出和私戳. Among the fo ...
- 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 ...
- 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 ...
- 3/100. Merge Two Binary Trees
将两个二叉树相同位置的数值相加,相加的方法一样,则使用递归法挨个相加即可. # Definition for a binary tree node. # class TreeNode: # def _ ...
最新文章
- 【java】兴唐第十八节课
- JAVA 面向对象
- Java实现数据库表结构导出到Excel
- linux下的代码比较工具下载,linux下的代码工具比较
- 如何设计一门语言(三)——什么是坑(面向对象和异常处理)
- 腾讯正式加入OCP阵营,拥抱全球开源生态圈
- python类的使用(类定义,构造器,类属性,方法)
- Percona Server for MySQL 5.5.30-30.2
- openshift_OpenShift Origin中的Kubernetes Spark运算符(第1部分)
- 2019 第八/九周/十周 开发笔记
- Android第二十课 解决Logcat无法输出调试信息
- visual studio 2015安装教程
- javaSE开发智能问答机器人项目
- 「网络安全」将会是下一个风口?这个“下饭神剧”值得一看!
- Linux搜索日志关键字的2种方法
- Codeforces 1144 D
- word中统一修改mathtype公式和大小对应
- 大数据就业:学完大数据怎样就业
- qemu 加载ubuntu
- android游戏手柄,没手柄也不怕 键盘玩Android游戏攻略