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:

  1. Input:{1,2,3,4},{1,3,2,#,#,#,4}
  2. Output:true
  3. Explanation:
  4. 1 1
  5. / \ / \
  6. 2 3 and 3 2
  7. / \
  8. 4 4
  9. are identical.

Example 2:

  1. Input:{1,2,3,4},{1,3,2,4}
  2. Output:false
  3. Explanation:
  4. 1 1
  5. / \ / \
  6. 2 3 and 3 2
  7. / /
  8. 4 4
  9. 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相似,只是多一种组合。

代码:

  1. /**
  2. * Definition of TreeNode:
  3. * public class TreeNode {
  4. * public int val;
  5. * public TreeNode left, right;
  6. * public TreeNode(int val) {
  7. * this.val = val;
  8. * this.left = this.right = null;
  9. * }
  10. * }
  11. */
  12.  
  13. public class Solution {
  14. /**
  15. * @param a: the root of binary tree a.
  16. * @param b: the root of binary tree b.
  17. * @return: true if they are tweaked identical, or false.
  18. */
  19. public boolean isTweakedIdentical(TreeNode a, TreeNode b) {
  20. if (a == null && b == null) {
  21. return true;
  22. }
  23. else if (a != null && b != null) {
  24. return (a.val == b.val && isTweakedIdentical(a.left, b.left)
  25. && isTweakedIdentical(a.right, b.right))
  26. ||(a.val == b.val && isTweakedIdentical(a.left, b.right)
  27. && isTweakedIdentical(a.right, b.left));
  28. }
  29. else {
  30. return false;
  31. }
  32. }
  33. }

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. Redis持久化介绍

    所有的数据都存在内存中,从内存当中同步到硬盘上,这个过程叫做持久化过程. 持久化操作,两种方式:rdb方式.aof方式,可以单独使用或者结合使用. 使用方法: rdb持久化方法:在指定的时间间隔写入硬 ...

  2. Json对象扩展

    1.JSON.stringify(obj/arr) js对象(数组)转换为json对象(数组) 2.JSON.parse(json) json对象(数组)转换为js对象(数组)

  3. 如何成功安装旧版本火狐,成功安装firebug和firepath插件

    很久不用火狐了,为了练习selenium定位浏览器等操作,下载了火狐浏览器,新版火狐已经没有firebug和firepath等插件,无法使用,只有安装老版本的火狐:*****安装好之后立刻设置不让他自 ...

  4. git删除和提交

    //删除git分支git branch -D BranchNamegit branch -r -D origin/BranchNamegit push origin -d BranchName//提交 ...

  5. TestFlight 测试

    问题1:iOS提交TestFlight测试显示缺少合规证明 解决:有的时候testFlight会显示@“缺少合规证明” 最简单的解决办法就是点击文字前边的黄色标识符,会询问你是否加密,选择相应选项就可 ...

  6. Check SQL Server Deadlock

    Sometimes a script keeps running for a long time and can't stop, then a db blocking is occurring. We ...

  7. Leetcode: The Maze III(Unsolved Lock Problem)

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  8. 接口自动化测试持续集成--Soapui接口测试

    接口测试目前笔者掌握的工具有三种: 一.python+requests+jenkins,优点:代码实现接口测试,对测试代码书写比较自由等:缺点:需要测试者需要有一定的代码基础: 二.jmeter+an ...

  9. Java -- 基于JDK1.8的ArrayList源码分析

    1,前言 很久没有写博客了,很想念大家,18年都快过完了,才开始写第一篇,争取后面每周写点,权当是记录,因为最近在看JDK的Collection,而且ArrayList源码这一块也经常被面试官问道,所 ...

  10. No module named 'pip._internal'

    报错: Traceback (most recent call last):File "/home/myuser/.local/bin/pip", line 7, in <m ...