572. Subtree of Another Tree 大树里包括小树
[抄题]:
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this node's descendants. The tree s could also be considered as a subtree of itself.
Example 1:
Given tree s:
3
/ \
4 5
/ \
1 2
Given tree t:
4
/ \
1 2
Return true, because t has the same structure and node values with a subtree of s.
Example 2:
Given tree s:
3
/ \
4 5
/ \
1 2
/
0
Given tree t:
4
/ \
1 2
Return false.
[暴力解法]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
- 两个点直接相等是两棵树相等的特殊情况,下次注意
 
[思维问题]:
只会写判断树的思路,不知道还有判断点的步骤, 二者需要分开
[一句话思路]:
判断树和判断点分开
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 布尔型函数必须有不在括号中的默认值,注意下
 - 调用点的traverse也是用的递归
 
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
- 调用点的traverse也是用的递归
 
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
判断点是否相等:必须要左右都相等才行
public boolean isSame(TreeNode s, TreeNode t) {
        //both null
        if (s == null && t == null) {
            return true;
        }
        //one is null
        if (s == null || t == null) {
            return false;
        }
        //false
        if (s.val != t.val) {
            return false;
        }
        //default
        return isSame(s.left, t.left) && isSame(s.right, t.right);
    }
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
//corner case
if (s == null) {
return false;
}
if (isSame(s,t)) {
return true;
}
return isSubtree(s.left, t) || isSubtree(s.right, t);
} public boolean isSame(TreeNode s, TreeNode t) {
//both null
if (s == null && t == null) {
return true;
}
//one is null
if (s == null || t == null) {
return false;
}
//false
if (s.val != t.val) {
return false;
}
//default
return isSame(s.left, t.left) && isSame(s.right, t.right);
}
}
572. Subtree of Another Tree 大树里包括小树的更多相关文章
- 572. Subtree of Another Tree
		
Problem statement: Given two non-empty binary trees s and t, check whether tree t has exactly the sa ...
 - LeetCode 572. Subtree of Another Tree (是否是另一个树的子树)
		
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...
 - 572. Subtree of Another Tree(easy)
		
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...
 - 【leetcode】572. Subtree of Another Tree
		
题目如下: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...
 - [LC] 572. Subtree of Another Tree
		
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...
 - 【easy】572. Subtree of Another Tree
		
判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等) 有两种方法: 方法二:递归写法 //方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树 //方法二 ...
 - LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40
		
572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 ...
 - 阻止a标签跳转四种方法 兼容各大浏览器(包括IE)
		
阻止a标签跳转四种方法 兼容各大浏览器(包括IE) HTML <!--第一种--> <a href="javascript:;">我不会被跳转</a& ...
 - 【LeetCode】572. 另一个树的子树 Subtree of Another Tree(Python & Java)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 方法二:DFS + DFS 方法三 ...
 
随机推荐
- db_recovery_file_dest_size 修改大一点及删除归档日志 |转|
			
今天给客户测 试问题,让客户把数据发过来了.解压缩后一看,他们还是用的oracle 815版本的(他们exp导出时,带了导出日志,从导出日志中看出来是oracle 815版本的),不过没有关系,低版本 ...
 - angular-ui-bootstrap的弹出框定义成一个服务的实践(二)
			
定义一个弹出框的服务:alert_box defiine(["app"],function(mainapp){ mainapp.controller('ModalInstanceC ...
 - Hadoop的简单序列化框架
			
Hadoop提供了一个加单的序列化框架API,用于集成各种序列化实现.该框架由Serialization实现. 其中Serialization是一个接口,使用抽象工厂的设计模式,提供了一系列和序列化相 ...
 - SharePoint2013 Online中InfoPath 无法调用WebService
			
传说微软office365中国区服务器已经迁移到国内,试了下速度果然比之前快了很多,不过随后测试了个简单的功能,还是直接被打击了. 准备在online版本中做一个简单的报销流程测试测试,于是先用Inf ...
 - GDB调试字符数组时指针和数组区别的体现
			
测试ftell函数时发现报错,先贴源码 // File Name: ftell.c #include <stdio.h> #include <stdlib.h> int mai ...
 - linux 查看字体
			
fc-list #字体列表 fc-list :lang=zh #中文字体 fc-match -v "字体名" # 查看字体详情
 - java web 程序---刷新页面次数
			
<%! int count=0; %> <% count++; session.setAttribute("count",count); out.print(&q ...
 - Oracle查询结果中的日期格式显示到毫秒数,如何去掉多余的数
			
@Temporal(TemporalType.TIMESTAMP) @Column(name="createTime",nullable=false) private Date c ...
 - 3D角色渲染到2D界面上
			
using UnityEngine; using System.Collections; using System.Collections.Generic; using Carrie.Net; usi ...
 - python学习 (三十三)  Modules
			
1: 方法一: 导入整个模块 import math class ModulesDemo(): def builtin_modules(self): print(math.sqrt()) m = Mo ...