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 ...
随机推荐
- 包的初识和进阶&异常处理
包 包是一种通过使用‘.模块名’来组织python模块名称空间的方式. 1. 无论是import形式还是from...import形式,凡是在导入语句中(而不是在使用时)遇到带点的,都要第一时间提高警 ...
- .Net Core知识点
1:const,readonly,和get访问器,三者都可在自己的生命域里赋值,但是编译器也是可以在构造函数里进行初始化赋值的 2:Debugger.IsAttached 属性 http://msdn ...
- DAX/PowerBI系列 - 关于时间系列 - 时间相关数值比较 - 用非自带函数
DAX/PowerBI系列 - 关于时间系列 - 时间相关数值比较 - 用非自带函数 文末有彩蛋,解决蛋疼问题 难度: ★★☆☆☆(2星) 适用范围: ★★★☆☆(3星) 概况: 基于时间的汇总可能是 ...
- python 使用 PIL 和 matplotlib 获取图片像素点处理之后再写入
python 版本 3.x 首先安装 PIL 由于PIL仅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基础上创建了兼容的版本,名字叫Pillow,支持最新Python 3.x,又 ...
- TCP 数据传输工具类
package com.ivchat.test.propertysystem.util; import java.io.BufferedReader;import java.io.ByteArrayO ...
- OOA/OOD/OOP
转载自https://www.cnblogs.com/zzyoucan/p/3576932.html Object-Oriented Analysis:面向对象分析方法 是在一个系统的开发过程中进行了 ...
- Linux启动顺序、运行级别及开机启动
一.启动顺序 当我们经过BIOS引导,并选择了Linux作为准备引导的操作系统后,接下来的执行顺序如下:1.加载并执行内核 第一个被加载的东西就是内核.然后把内核在内存中解压缩,就可以开始运行了.2. ...
- CentOS 7 配置DHCP中继代理服务
DHCP服务器只作用于局域网同一网段内,客户端是通过广播消息来获得DHCP服务器响应后才能得到IP地址的,但广播消息不能跨越子网,那么如何让客户端获取到DHCP服务器提供的IP地址呢?这就是DHCP中 ...
- laravel composer
composer config -g repo.packagist composer https://packagist.phpcomposer.com 改安装包的全局镜像网址
- Lua 判断文件类型为wav
[1]应用示例 文件类型为wav格式 -- 判断文件类型 local function isType(filename) local res = string.match(filename, &quo ...