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 方法三 ...
随机推荐
- 【转】电信100M光纤无线下载速度仅为5MB/秒的困惑
原文网址:http://itbbs.pconline.com.cn/50463999.html 在江苏电信官方测速网站测速的.1.光猫F460有线连接至笔记本,下载速度为12MB/秒左右:2.F460 ...
- vue-router做路由拦截时陷入死循环
今天分享一下使用vue-router做路由拦截时遇到的坑. 需要提前了解的api 1:router.beforeEach( to , from ,next) ; to: Route: 即将要进入的目标 ...
- centos6.6安装SVN服务器(2015/3/7)
一.安装#yum install subversion 判断是否安装成功 [root@]# svnserve --version有了SVN软件后还需要建立SVN库.#mkdir /opt/svn/ ...
- bzoj 4530 [Bjoi2014]大融合——LCT维护子树信息
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4530 LCT维护子树 siz .设 sm[ ] 表示轻儿子的 siz 和+1(1是自己的si ...
- CentOS 6.5 下MySql主从、主主配置
参考网站: http://blog.csdn.net/faye0412/article/details/6280761 http://blog.csdn.net/kk185800961/article ...
- Linux 根文件系统目录结构
/:根目录 /bin:linux的常用命令 /sbin:linux的常用命令 /lib:库文件(so.elf) /etc:系统配置文件和脚本文件 /sys:驱动相关的信息 /dev:设备节点目录 /p ...
- Bash命令查找本机公网IP
用Bash命令查找本机公网IP wget -qO - http://ipecho.net/plain; echo
- Charles 协助解决 metaweblog 排序问题
Charles 是 http代理抓包工具,可有效用于手机客户端网络抓包,详见Charles安装说明.这里使用使用Charles的请求转发功能调试metaweblog的最近博文排序功能. 由于OpenL ...
- sorl基本原理
索引原理 场景:小时候我们都使用过新华字典,妈妈叫你翻开第38页,找到“坑爹”所在的位置,此时你会怎么查呢?毫无疑问,你的眼睛会从38页的第一个字开始从头至尾地扫描,直到找到“坑爹”二字为止.这种搜索 ...
- hadoop Shuffle Error OOM错误分析和解决
在执行Reduce Shuffle的过程中,偶尔会遇到Shuffle Error,但是重启任务之后,Shuffle Error会消失,当然这只是在某些特定情况下才会报出来的错误.虽然在每次执行很短的时 ...