245. Subtree【LintCode java】
Description
You have two very large binary trees: T1
, with millions of nodes, and T2
, with hundreds of nodes. Create an algorithm to decide if T2
is a subtree of T1
.
A tree T2 is a subtree of T1 if there exists a node n in T1 such that the subtree of n is identical to T2. That is, if you cut off the tree at node n, the two trees would be identical.
Example
T2 is a subtree of T1 in the following case:
1 3
/ \ /
T1 = 2 3 T2 = 4
/
4
T2 isn't a subtree of T1 in the following case:
1 3
/ \ \
T1 = 2 3 T2 = 4
/
4
解题:判断一个二叉树是不是另一个二叉树的问题。思路还是很清晰的,但是有些细节还是要注意下的。两个递归,一个用来判断两个树是否完全一样,这个只要不一样就返回false。
还有一个递归函数是用来返回最终结果的。区别在于,如果第二个函数判断不等,还有继续往T2的子树探索,知道找到一个与T1完全相等的,或者一直到最后也不存在这样的函数。
比较容易错的是,在判断结点值的时候,要先判断结点是不是null(判空)。
代码如下:
/**
* 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 T1: The roots of binary tree T1.
* @param T2: The roots of binary tree T2.
* @return: True if T2 is a subtree of T1, or false.
*/
public boolean equal(TreeNode T1, TreeNode T2){
if(T1 == null && T2 == null){
return true;
}else if(T1 == null && T2 != null || T1 != null && T2 == null||T1.val != T2.val){
return false;
}else{
return equal(T1.left, T2.left) && equal(T1.right, T2.right);
}
}
public boolean isSubtree(TreeNode T1, TreeNode T2) {
// write your code here
if(T2 == null)
return true;
if(T1 == null)
return false;
if(equal(T1 , T2))
return true;
else return isSubtree(T1.left, T2)||isSubtree(T1.right, T2);
}
}
245. Subtree【LintCode java】的更多相关文章
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 423. Valid Parentheses【LintCode java】
Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
- 415. Valid Palindrome【LintCode java】
Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...
- 413. Reverse Integer【LintCode java】
Description Reverse digits of an integer. Returns 0 when the reversed integer overflows (signed 32-b ...
随机推荐
- Python嵌套列表去重
raw_list = [ [ 'CS_SUPP_INFO', 'A', '1'], [ 'CS_SUPP_INFO', '1', 'A'], [ 'CS_SUPP_INFO', '1', 'A'], ...
- 2018 Wannafly summer camp Day8--连通块计数
连通块计数 描述 题目描述: 小 A 有一棵长的很奇怪的树,他由 n 条链和 1 个点作为根构成,第 i条链有 ai 个点,每一条链的一端都与根结点相连. 现在小 A 想知道,这棵长得奇怪的树有多少 ...
- HDU 2065 "红色病毒"问题(生成函数)
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- Archlinux下安装微信小程序开发工具
由于微信小程序没有Linux版本,所以需要用wine来跑 一.安装wine sudo pacman -S wine 二.安装nwjs-sdk 微信开发工具包基于nwjs-sdk #没有wget就先安装 ...
- 获取地图的信息到input里
在最近项目中,我接触了百度地图的API写法,对其中的代码有了一点兴趣,所以我在完成任务后,在办公室里学习了百度地图的相关引用,并申请了服务秘钥: E7PCho0sv3FdzmjC901ttP0HrS9 ...
- 能够还原jQuery1.8的toggle的功能的插件
下面这个jQuery插件能够还原1.8的toggle的功能,如果你需要,可以直接把下面这段代码拷贝到你的jQuery里面,然后跟平时一样使用toggle的功能即可. //toggle plugin f ...
- 第8章 ZooKeeper操作
目录 8.1 集群环境搭建 1.上传ZooKeeper安装文件 2.编写配置文件 3.拷贝ZooKeeper安装信息到其它节点 4.修改其它节点配置 5.启动ZooKeeper 6.查看启动状态 7. ...
- mt7620a拓展串口
mt7620a拓展串口 要修改的文件有两个: mt7620a.dtsi 进入/home/ihid/chaos_calmer/target/linux/ramips/dts/mt7620a.dtsi p ...
- JS第一周学习笔记整理
目录 JS正式课第一周笔记整理 JS正式课第一周笔记整理 webstorm : 代码编辑器 浏览器: 代码解析器: Git : 是一个工具;用于团队协作开发项目管理代码的工具:在工作中用git.svn ...
- ruby 爬虫爬取拉钩网职位信息,产生词云报告
思路:1.获取拉勾网搜索到职位的页数 2.调用接口获取职位id 3.根据职位id访问页面,匹配出关键字 url访问采用unirest,由于拉钩反爬虫,短时间内频繁访问会被限制访问,所以没有采用多线程, ...