15.Subtree of Another Tree(判断一棵树是否为另一颗树的子树)
Level:
Easy
题目描述:
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
思路分析:
判断t树是否为s树的子树,先在s中找到和t根节点相同的节点,然后从该节点出发判断是否存在与t完全相同的结构。
代码:
public class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int x){
val=x;
}
}
public class Solution{
public boolean isSubtree(TreeNode s,TreeNode t){
if(s==null||t==null)
return false;
return checkTree(s,t)||isSubtree(s.left,t)||isSubtree(s.right,t);//找到与t根节点相同的节点,然后开始进行判断
}
public boolean checkTree(TreeNode s,TreeNode t){
if(s==null&&t==null) //同时为null证明结构一样
return true;
if(s==null||t==null) //如果任意一个不为空,代表结构不一样
return false;
return (s.val==t.val)&&checkTree(s.left,t.left)&&checkTree(s.right,t.right);//这是判断结构是否相同的条件,对应节点值相同,并且左右子树对应的结构也要相同。
}
}
15.Subtree of Another Tree(判断一棵树是否为另一颗树的子树)的更多相关文章
- [LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树
将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树. 用list记录所有子树的序列化,和目标树比较. List<String> list = new ArrayList< ...
- 南昌网络赛 Distance on the tree 主席树+树剖 (给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数。)
https://nanti.jisuanke.com/t/38229 题目: 给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数. #include <bits/stdc++.h ...
- LeetCode 100. Same Tree 判断两棵二叉树是否相等 C++
Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...
- 【easy】572. Subtree of Another Tree
判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等) 有两种方法: 方法二:递归写法 //方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树 //方法二 ...
- [LeetCode] Subtree of Another Tree 另一个树的子树
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(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 方法二:DFS + DFS 方法三 ...
- [LeetCode] Same Tree 判断相同树
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- 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 ...
- LeetCode 101. Symmetric Tree 判断对称树 C++
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
随机推荐
- MessageBox如何输出整数
int cx=10;CString s;s.Format(_T("整数是:%d"),cx);MessageBox(s);
- nginx isis
Nginx+IIS简单的部署 随着互联网项目用户访问量不断上升,单点web服务器是无法满足大型高并发高负载的业务处理的,为了给web服务器做负载均衡方案,打算采用Nginx搭建负载均衡服务器,把用 ...
- 【转载】基于TINY4412的Andorid开发-------简单的LED灯控制
阅读目录(Content) 一.编写驱动程序 二.编写代码测试驱动程序 三.编写HAL代码 四.编写Framework代码 五.编写JNI代码 六.编写App 参考资料: <Andriod系统源 ...
- DAY16-Django之model
Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...
- hadoop报错java.io.IOException: Incorrect configuration: namenode address dfs.namenode.servicerpc-address or dfs.namenode.rpc-address is not configured
不多说,直接上干货! 问题详情 问题排查 spark@master:~/app/hadoop$ sbin/start-all.sh This script is Deprecated. Instead ...
- Windows安装memcached图文教程(转)
一.下载Memercached For Windows 二.安装步骤 1.解压到指定目录,如:C:\Memcached\memcached-win32-1.4.4-14. 2.用cmd打开命令窗口,转 ...
- PLSQL语法
Procedural Language和SQL的结合体.通过增加变量.控制语句,使我们可以写些逻辑更加复杂的数据库操作 语句框架组成 declare – 可选 声明各种变量或游标的地方. begin ...
- 【转】手把手教你用Strace诊断问题
原博客地址:http://huoding.com/2015/10/16/474 早些年,如果你知道有个 strace 命令,就很牛了,而现在大家基本都知道 strace 了,如果你遇到性能问题求助别人 ...
- R: 常用操作:
################################################### #清除所有变量: rm(list=ls()) #查看变量类型 getwd() setwd() i ...
- jQuery选择器和选取方法.RP
我们已经使用了带有简单Css选择器的jQuery选取函数:$().现在是时候深入了解jQuery选择器语法,以及一些提取和扩充选中元素集的方法了. 一.jQuery选择器 在CSS3选择器标淮草案定义 ...