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. numpy(五)

    排序: x=np.array([2,5,6,2,3,5]) np.sort(x)  不改变原数组 x.sort() 改变原数组 i=np.argsort(x) 返回排序好的索引值 x[i] 使用花哨索 ...

  2. pkg-config 详解

    转载自:https://blog.csdn.net/newchenxf/article/details/51750239 1 什么是pkg-config pkg-config是一个linux下的命令, ...

  3. 使用easyui搭建网页架子

    使用踩坑: 一.弹出框上datagrid第二次加载数据,必须在显示状态,datagrid加载数据才会渲染,否则是空白 $('#xq_selKs').window('open').window('cen ...

  4. MQTT----物联网常用的消息队列协议

    MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的“轻量级”通讯协议,该协议构建 ...

  5. Mysql 5.7优化

    为了达到数据库胡最佳性能 1. 普通用户通过配置软件与硬件来实现 2. 高级用户会寻求机会改善MySQL本身,开发自己的数据存储引擎,硬件应用. 在数据库层面的优化 1. 表设计,通常列有适合的数据类 ...

  6. Mac下vim7.4+vimgdb让vim支持gdb源码调试

    下载vimgdb https://github.com/cpiger/vimgdb-for-vim7.4 下载vim7.4源码 将两个文件或者文件夹放到同一个目录解压 tar xjvf vim-7.4 ...

  7. hybrid简单了解

    技术点总有它的来由. 文章概要: 1.hybrid 基本概念 2.前端和客户端的交互 3.前端和客户端的交互实现 4.前端交互实现关注点 5.小结 1.hybrid 基本概念 ⑴.什么是hybrid? ...

  8. DDD关键知识点整理汇总

    创建领域对象采用构造函数或者工厂,如果用工厂时需要依赖于领域服务或仓储,则通过构造函数注入到工厂: 一个聚合是由一些列相联的Entity和Value Object组成,一个聚合有一个聚合根,聚合根是E ...

  9. Install Sudo for Debian

    $ su $ apt-get install sudo $ vim /etc/sudoers 1 2 3 在文本中添加: "username" ALL=(ALL) ALL 1 保存 ...

  10. VS2015右键集成TortoiseGit

    先上效果 再说步骤 1.安装VS TortoiseGit等~~ 2.以外部工具方式调用TortoiseGit 3.在VS中设置右键菜单 在菜单栏下方右键,选择自定义 在弹出窗口中选择,命令->上 ...