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 ...
随机推荐
- 使你的ActiveX控件执行时不弹出安全性提示(转载)
我们编写一个ActiveX控件在IE中运行,一般会弹出一个安全提示,如何避免这种情况?下面是我在参考前人的文章后,总结出“在浏览器中执行时不弹出警告的ActiveX控件”的两种编写方法,予以备忘.注意 ...
- linux 重启 启动 apache服务
如何使用service httpd restart,不成功的话,直接去apache文件目录里去找可执行文件,执行启动. 一般apache目录 ./usr/local/httpd/bin ,在bin目 ...
- Process.waitFor()导致主线程堵塞问题
今日开发的时候使用jdk自带的运行时变量 RunTime.getRunTime() 去执行bash命令.因为该bash操作耗时比较长,所以使用了Process.waitFor()去等待子线程运行结束. ...
- NodeJ node.js Koa2 跨域请求
Koa2 .3 跨域请求 Haisen's 需求分析 (localhost:8080 = 前端 [请求] localhost:8081 = 服务器 ) 1.一个前台 一个服务器 前台 ...
- 【Django笔记四】Django2.0中的表单
一.环境版本信息: 操作系统:windows10 Django版本:2.0.5 Python版本:3.6.4 Mysql版本: 5.5.53 安装mysql 二.基础信息 1.App中的模型mod ...
- Tesseract-OCR 自动生成识别库的批处理
用Tesseract-OCR做识别库的时候,生成字典非常麻烦,就写了一个批处理,用来生成字典还是蛮方便的,希望大家有用,该批处理已经自动生成font_properties文件,各位无需手动创建 下载地 ...
- ABAP术语-Technical Object
Technical Object 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/18/1111205.html Generic term f ...
- tp3.2源码解析——入口文件
如果有人读这篇文章并跟着做的话,希望你能使用支持函数跳转的编辑器,还要善用var_dump和exit,对着源码去调试着看.跟着入口文件读,执行到哪里你看到哪里,对于那些不能一眼看出来的配置,则要记录下 ...
- 常用贴片三极管型号与丝印的对应关系(SOT23)
个人常用贴片三极管型号与丝印的对应关系(SOT23): 丝印:Y1 型号:8050,NPN型三极管 丝印:Y2 型号:8550,PNP型三极管 丝印:L6 ...
- 令人懊恼的阉割版fabric sdk功能缺失
按理说,fabric本身都是用golang开发的,那么fabric-sdk-go作为其亲儿子,功能应该是最为完善的.然而,与我们想法相左的是,golang版本的sdk反而是最不完备的,开发进度滞后,功 ...