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. 【托业】【怪兽】TEST04

    ❤ admit doing sth 承认做某事 ❤revelation n.揭露,揭示 ❤dazzling adj. 炫目的 ❤intentionally adv.刻意地 ❤metropolitan ...

  2. Servlet学习1

    1.首先在Tomcat的webapp目录下新建文件夹myWebapp,作为自己的web应用. 2.myWebapp下新建WEB-INF(必须这个名)目录,WEB-INF下新建classes目录放置se ...

  3. attr 和 prop的使用区别

    自带的属性,用prop:自定义的属性,用attr.

  4. supervise守护进程

    通过二进制supervise文件可以直接对进程进行守护 ./supervise -f  要守护的程序 -p  守护信息存储位置 例如: ./supervise -f  http_server -p s ...

  5. 短信利用weixin://connectToFreeWifi/?apKey=协议跳转到微信打开落地页h5

    微信门店wifi接口,任意站跳转,跳转二维码长按识别加粉,接口支持动态传参数,支持微信支付等特殊接口对接. 代码如下使用 <head> <meta charset="utf ...

  6. 关于12c安装后打补丁

    根据ID:1454618.1,可在MOS上查到相关版本详细的补丁版本号. 补丁安装过程参考补丁文档,以下是大致过程. 1. 更新OPatch2. 创建OCM应答文件(每个节点)$ORACLE_HOME ...

  7. JavaScript命名规范基础及系统注意事项

    前端代码中的自定义变量命名           命名方法:     1.驼峰 2.下划线连接           对于文件名,我们一般采用小写字母+下划线的形式     为什么?因为在window下a ...

  8. STM32F103单片机解密资料

    STM32F103单片机解密资料下载 一.STM32 32位ARM Cortex MCU 基于ARM® Cortex® M 处理器内核的 32位闪存微控制器STM32产品家族,为MCU用户开辟了一个全 ...

  9. CentOS7编译安装SVN(subversion1.9.7)

    参考连接0:http://www.programering.com/a/MDMzYDMwATg.html参考连接1:http://www.zsythink.net/archives/13180.系统信 ...

  10. sitecore开发入门之如何在代码中获取SITECORE图像URL

    using Sitecore; using Sitecore.Data.Items; using Sitecore.Resources.Media; public string GetUrl() { ...