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(判断一棵树是否为另一颗树的子树)的更多相关文章

  1. [LeetCode]Subtree of Another Tree判断一棵树是不是另一棵树的子树

    将树序列化为字符串,空节点用符号表示,这样可以唯一的表示一棵树. 用list记录所有子树的序列化,和目标树比较. List<String> list = new ArrayList< ...

  2. 南昌网络赛 Distance on the tree 主席树+树剖 (给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数。)

    https://nanti.jisuanke.com/t/38229 题目: 给一颗树,m次查询ui->vi这条链中边权小于等于ki的边数. #include <bits/stdc++.h ...

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

  4. 【easy】572. Subtree of Another Tree

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

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

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

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

  7. [LeetCode] Same Tree 判断相同树

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

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

  9. LeetCode 101. Symmetric Tree 判断对称树 C++

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

随机推荐

  1. vsftpd 被动模式与主动模式

    vsftpd 被动模式与主动模式 VSFTP文件与目录/usr/sbin/vsftp vsftp的主程序/etc/rc.d/init.d/vsftp vsftp的启动脚本/etc/vsftpd/vsf ...

  2. opencv 美白磨皮人脸检测<转>

    1. 简介 这学期的计算机视觉课,我们组的课程项目为“照片自动美化”,其中我负责的模块为人脸检测与自动磨皮.功能为:用户上传一张照片,自动检测并定位出照片中的人脸,将照片中所有的人脸进行“磨皮”处理, ...

  3. 关于大数据领域各个组件打包部署到集群运行的总结(含手动和maven)(博主推荐)

    对于这里的打包,总结: (1)     最简单的,也是为了适应公司里,还是要用maven,当然spark那边sbt,maven都可以.但是maven居多. Eclipse/MyEclipse下如何Ma ...

  4. Internet Intranet Extranet

    Internet: There's only one of it, and you're on it now. Intranet: An internal network local to a com ...

  5. lucene、solr中的日期衰减方法-------function query --尚未测试在solr4.8

    经常有一种情景是这样的:我们索引了N年的文章,而查询时候无论直接用相关度.或者用时间排序,都是比较鲁莽的:我们想要一种既要相关度比较高,又要时间上比较新的文章. 这时候的解决办法就是,自定义日期衰减的 ...

  6. elasticsearch2.x ik插件

    先来一个标准分词(standard),配置如下: curl -XPUT localhost:/local -d '{ "settings" : { "analysis&q ...

  7. 基于unity3d的RRT算法路径规划

  8. jmeter CSV Data数据中带有逗号解决方法

    今天用jmeter做性能测试,由于参数的数据中含有逗号,一直失败,尝试了几次终于成功,先写下经验 首先看设置 E:\apache-jmeter-2.12\bin\litaojunzb.csv文件格式如 ...

  9. STM32 C++编程 003 USART(串口)类

    使用 C++ 语言给 STM32 编写一个 Usart 类 我使用的STM32芯片:STM32F103ZET6 我们使用的STM32库版本:V3.5.0 注意: 想学习本套 STM32 C++编程 的 ...

  10. GCD 学习(八)dispatch_semaphore

    dispatch_semaphore 信号量基于计数器的一种多线程同步机制.在多个线程访问共有资源时候,会因为多线程的特性而引发数据出错的问题.     dispatch_queue_t queue ...