Lintcode470-Tweaked Identical Binary Tree-Easy
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的更多相关文章
- 【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 ...
- 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 ...
- 93. Balanced Binary Tree [easy]
Description Given a binary tree, determine if it is height-balanced. For this problem, a height-bala ...
- 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 ...
- Invert Binary Tree(easy)
1.直接把递归把左右子树翻转即可 AC代码: /** * Definition for a binary tree node. * struct TreeNode { * int val; * Tre ...
- [LintCode] Identical Binary Tree 相同二叉树
Check if two binary trees are identical. Identical means the two binary trees have the same structur ...
- Identical Binary Tree
Check if two binary trees are identical. Identical means the two binary trees have the same structur ...
- LintCode: Identical Binary Tree
C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...
- [leetcode] 111.Mininum Depth of Binary Tree (Easy)
原题 寻找二叉树最短深度 这里用了dfs,beat 100%,4ms class Solution { public: int minDepth(TreeNode *root, int minNum ...
- [leetcode] 543. Diameter of Binary Tree (easy)
原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...
随机推荐
- Redis持久化介绍
所有的数据都存在内存中,从内存当中同步到硬盘上,这个过程叫做持久化过程. 持久化操作,两种方式:rdb方式.aof方式,可以单独使用或者结合使用. 使用方法: rdb持久化方法:在指定的时间间隔写入硬 ...
- Json对象扩展
1.JSON.stringify(obj/arr) js对象(数组)转换为json对象(数组) 2.JSON.parse(json) json对象(数组)转换为js对象(数组)
- 如何成功安装旧版本火狐,成功安装firebug和firepath插件
很久不用火狐了,为了练习selenium定位浏览器等操作,下载了火狐浏览器,新版火狐已经没有firebug和firepath等插件,无法使用,只有安装老版本的火狐:*****安装好之后立刻设置不让他自 ...
- git删除和提交
//删除git分支git branch -D BranchNamegit branch -r -D origin/BranchNamegit push origin -d BranchName//提交 ...
- TestFlight 测试
问题1:iOS提交TestFlight测试显示缺少合规证明 解决:有的时候testFlight会显示@“缺少合规证明” 最简单的解决办法就是点击文字前边的黄色标识符,会询问你是否加密,选择相应选项就可 ...
- Check SQL Server Deadlock
Sometimes a script keeps running for a long time and can't stop, then a db blocking is occurring. We ...
- 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 ...
- 接口自动化测试持续集成--Soapui接口测试
接口测试目前笔者掌握的工具有三种: 一.python+requests+jenkins,优点:代码实现接口测试,对测试代码书写比较自由等:缺点:需要测试者需要有一定的代码基础: 二.jmeter+an ...
- Java -- 基于JDK1.8的ArrayList源码分析
1,前言 很久没有写博客了,很想念大家,18年都快过完了,才开始写第一篇,争取后面每周写点,权当是记录,因为最近在看JDK的Collection,而且ArrayList源码这一块也经常被面试官问道,所 ...
- No module named 'pip._internal'
报错: Traceback (most recent call last):File "/home/myuser/.local/bin/pip", line 7, in <m ...