[抄题]:

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]:

  1. 两个点直接相等是两棵树相等的特殊情况,下次注意

[思维问题]:

只会写判断树的思路,不知道还有判断点的步骤, 二者需要分开

[一句话思路]:

判断树和判断点分开

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 布尔型函数必须有不在括号中的默认值,注意下
  2. 调用点的traverse也是用的递归

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

  1. 调用点的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 大树里包括小树的更多相关文章

  1. 572. Subtree of Another Tree

    Problem statement: Given two non-empty binary trees s and t, check whether tree t has exactly the sa ...

  2. 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 ...

  3. 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 ...

  4. 【leetcode】572. Subtree of Another Tree

    题目如下: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...

  5. [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 ...

  6. 【easy】572. Subtree of Another Tree

    判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等) 有两种方法: 方法二:递归写法 //方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树 //方法二 ...

  7. LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40

    572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 ...

  8. 阻止a标签跳转四种方法 兼容各大浏览器(包括IE)

    阻止a标签跳转四种方法 兼容各大浏览器(包括IE) HTML <!--第一种--> <a href="javascript:;">我不会被跳转</a& ...

  9. 【LeetCode】572. 另一个树的子树 Subtree of Another Tree(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 方法二:DFS + DFS 方法三 ...

随机推荐

  1. 记一次 FastAdmin CMS 内容提示空的问题

    记一次 FastAdmin CMS 内容提示空的问题 有小伙伴反馈 FastAdmin CMS 安装后出现内容有文字,但提示错误 的问题. 我在本地重新安装测试并没有发现这个问题,一切正常,编辑器也可 ...

  2. 再探VIM配置

    再探VIM配置 最初找到这个发行版spf13-vim,在ubuntu上用的还比较方便,有很多插件:最近在mac上用,总是不兼容vim,用brew安装了最新的vim,还是跟系统不兼容,总是有问题,于是就 ...

  3. PyBrain库的example之NFQ流程图分析

    PyBrain库的example之NFQ流程图分析 如下是测试程序.主要分析doEpisode和learn两个函数. #!/usr/bin/env python __author__ = 'Thoma ...

  4. 军哥LNMP优化

    http://bbs.vpser.net/thread-8914-1-1.html http://www.zxsdw.com/index.php/archives/881/ 修改/usr/local/ ...

  5. saiku3.8二次开发项目搭建(非maven)

    参考文章:http://blog.csdn.net/gsying1474/article/details/51603535 本文大部分参考了上面的博文,这里只是做一个记录,由于本人maven能力有限, ...

  6. Unit05: 实战技巧 、 资费列表 、 拦截器

    Unit05: 过滤器解决表单写中文乱码.拦截器 1. 使用过滤器解决表单中文参数值乱码问题 注意: a. 表单提交方式必须为POST. b. 过滤器的编码应该与浏览器端设置的编码一致. 2. 拦截器 ...

  7. laravel里面的控制器笔记

    看了下教程,总结了下,大概分两种 一般的controller restful的controller 单独绑定action的route为 Route::get('user/{id}', 'UserCon ...

  8. Java中如何查看一个类依赖的包

    Java中如何查看一个类依赖的包 如图, 我如何知道JSONArray是依赖的哪一个包呢,这里有两个json-lib包?   测试语句:   public static void main(Strin ...

  9. github打不开问题

    修改host 185.31.17.184 github.global.ssl.fastly.net 207.97.227.239 http://github.com 65.74.177.129 htt ...

  10. STM32=LWIP

    https://blog.csdn.net/zouw96/article/details/8443141