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. jsp的环境搭建

    JSP : 动态网页 一.静态和动态的区别: 1.是否会随着时间.地点.用户操作的改变而改变. 2.动态网页需要使用服务器端的脚本语言(JSP) 二.BS CS 1.CS:QQ.微信.CS游戏. 缺点 ...

  2. docker命令汇总

    docker命令汇总 序号 类别 简述 命令 功能 说明 1 整体管理 安装 yum install docker-engine centos上安装docker Ubuntu上安装dockerapt- ...

  3. oracle树形结构全路径查询

    很实用的语法,父子节点通过id与patientId来关联,知道子节点的id,想查出所有的父节点: START WITH ...CONNECT BY ... SELECT T2.ORG_FULLNAME ...

  4. requests库的基本使用

    1.发送get请求 import requests # response=requests.get('http://www.baidu.com') # 查看响应内容,返回的是已经解码的内容 # res ...

  5. 617A

    #include <stdio.h> int main() { int moves[5]={1,2,3,4,5}; int x; scanf("%d", &x) ...

  6. 3.3.1 MyBatis框架介绍

    MyBatis框架介绍 1. 什么是框架 (1) 框架是偷懒的程序员将代码进行封装, 之后进行重复使用的过程. (2) 框架其实是一个半成品, 以连接数据库为例, 连接数据库使用的驱动, url, 用 ...

  7. Kali Hydra SSL issue, xHydra (GUI version of Hydra) works just fine

    First find the source code. (https://is.gd/LlS5Sy) - Example search Once located you must download i ...

  8. ionic3 小记录

    cordova platform add ios@latest 安装最新ios ionic cordova build ios -- --buildFlag="-UseModernBuild ...

  9. JavaIO流——简单对文件的写入及读取(一)

    IO,即Input(输入)和Output(输出)的首字母缩写. 在编程语言的I/O类库中常使用流这个抽象概念.它代表任何有能力产出数据的数据源对象或者是与能力接收数据的接收端对象.“流”屏蔽了实际的I ...

  10. C语言中格式字符串

    C语言中格式字符串的一般形式为: %[标志][输出最小宽度][.精度][长度]类型, 其中方括号[]中的项为可选项. 一.类型 我们用一定的字符用以表示输出数据的类型,其格式符和意义下表所示: 字符  ...