470. Tweaked Identical Binary Tree

Check two given binary trees are identical or not. Assuming any number of tweaksare allowed. A tweak is defined as a swap of the children of one node in the tree.

Example

Example 1:

Input:{1,2,3,4},{1,3,2,#,#,#,4}
Output:true
Explanation:
1 1
/ \ / \
2 3 and 3 2
/ \
4 4 are identical.

Example 2:

Input:{1,2,3,4},{1,3,2,4}
Output:false
Explanation: 1 1
/ \ / \
2 3 and 3 2
/ /
4 4 are not identical.

Challenge

O(n) time

Notice

There is no two nodes with the same value in the tree.

思路:

tweaked identical tree 有两种情况,一种是b树是a树的tweak树(每个对应结点都交换),二是b树和a树完全相同。这两种情况为true。

思路和Lintcode469 Same tree相似,只是多一种组合。

代码:

/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/ public class Solution {
/**
* @param a: the root of binary tree a.
* @param b: the root of binary tree b.
* @return: true if they are tweaked identical, or false.
*/
public boolean isTweakedIdentical(TreeNode a, TreeNode b) {
if (a == null && b == null) {
return true;
}
else if (a != null && b != null) {
return (a.val == b.val && isTweakedIdentical(a.left, b.left)
&& isTweakedIdentical(a.right, b.right))
||(a.val == b.val && isTweakedIdentical(a.left, b.right)
&& isTweakedIdentical(a.right, b.left));
}
else {
return false;
}
}
}

Lintcode470-Tweaked Identical Binary Tree-Easy的更多相关文章

  1. 【leetcode】Minimum Depth of Binary Tree (easy)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  2. Leetcode 226. Invert Binary Tree(easy)

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

  3. 93. Balanced Binary Tree [easy]

    Description Given a binary tree, determine if it is height-balanced. For this problem, a height-bala ...

  4. LeetCode:104 Maximum Depth of Binary Tree(easy)

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  5. Invert Binary Tree(easy)

    1.直接把递归把左右子树翻转即可 AC代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tre ...

  6. [LintCode] Identical Binary Tree 相同二叉树

    Check if two binary trees are identical. Identical means the two binary trees have the same structur ...

  7. Identical Binary Tree

    Check if two binary trees are identical. Identical means the two binary trees have the same structur ...

  8. LintCode: Identical Binary Tree

    C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...

  9. [leetcode] 111.Mininum Depth of Binary Tree (Easy)

    原题 寻找二叉树最短深度 这里用了dfs,beat 100%,4ms class Solution { public: int minDepth(TreeNode *root, int minNum ...

  10. [leetcode] 543. Diameter of Binary Tree (easy)

    原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...

随机推荐

  1. 1.Qt字符编码

    1.给空间设置内容,有显示中文的,必须是utf-8编码: 2.从Qt得到的字符串,如果有中文,编码是utf-8,和Linux是一样的: 3.如果使用标准的C函数,如果有中文,是gbk编码: ANSI, ...

  2. 9、Flutter 实现 生成二维码

    1.加入依赖 在 pubspec.yaml 中 dependencies 节点下添加: dependencies: qr_flutter: ^ 2.引入代码 在需要细线二维码的 dart 类中引入依赖 ...

  3. 前端混淆--JavaScript Obfuscator

    引言: 前端代码是直接暴漏在浏览器中的,很多web攻击都是通过直接debug业务逻辑找到漏洞进行攻击,另外还有些喜欢“不劳而获”的分子暴力盗取他人网页简单修改后用来获利,总体上来说就是前端的逻辑太容易 ...

  4. 806. Number of Lines To Write String

    806. Number of Lines To Write String 整体思路: 先得到一个res = {a : 80 , b : 10, c : 20.....的key-value对象}(目的是 ...

  5. T-SQL语言基础(2)之SQL Server体系结构

    SQL Server 体系结构 SQL Server 实例 SQL Server 实例是指安装的一个 SQL Server 数据库引擎/服务.在同一台计算机上可以安装 SQL Server 的多个实例 ...

  6. 光圈,快门, 曝光,焦距, ISO,景深。

    光圈,快门, 曝光,焦距, ISO,景深. ISO(感光度)与图片质量 ISO -- 感光度,是一个曝光率极高的词,我们在超市买饼干的时候就可能会看见包装袋上写:本公司已通过ISO9001质量体系认证 ...

  7. Debian setup the time

    sudo gedit /etc/default/hwclock将 井HWCLOCKACCESS=yes 和 井HCTOSYS_DEVICE=rtc0 前的 井 符号去掉,再改 rtc0 为 rtc1 ...

  8. GIL

    GIL(Global Interpreter Look):全局解释器锁,为了避免线程竞争资源造成数据错乱. 其实每个py程序都必须有解释器参加,解释器就是一堆代码,就等于多线程要竞争同一个解释器的代码 ...

  9. WEB学习小笔记

    环境基于WIN10.IDEA最新版.JDK1.8.TOMCAT9 下面说的有错的地方希望指出,谢谢. STRUT2 1.在maven下的时候系统会系统创建一个叫做log4j的配置文件,但是到了这个版本 ...

  10. Java基础语法-Unicode、UTF-8、UTF-16

    1.Unicode(统一码.万国码),从名字里可以看出,unicode码表囊括世界上各国语言文字. unidode中包含17个代码级别,第一个代码级别又称作基本的多语言级别(码点从U+0000到U+F ...