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. 第三章 Java内存模型(下)

    锁的内存语义 中所周知,锁可以让临界区互斥执行.这里将介绍锁的另一个同样重要但常常被忽视的功能:锁的内存语义 锁的释放-获取建立的happens-before关系 锁是Java并发编程中最重要的同步机 ...

  2. VS调试程序时,程序出现异常,但VS不报错的解决方案

    在调试>异常> 里面把勾全勾上就行了!

  3. 原来windows里记事本的ansi编码就是GB2312啊,跟utf-8,unicode是不一样的。

    原来windows里记事本的ansi编码就是GB2312啊,跟utf-8,unicode是不一样的. 程序里的比如java的,Qt的string都是unicode的字符串,因此如果是你从文件中读取文字 ...

  4. apaache php 日记设计

    有个客户服务器是用apache搭建的,最近总是感觉站很慢,服务器很慢很卡,有时候甚至网 站都打不开,后来经过排查分析原来是里面的access.log和error.log这两个文件要经常上去看,和清理, ...

  5. nodejs读文件

    1.异步读取文件:var fs= require('fs'); // 从文件系统中读取请求的文件内容 fs.readFile(pathname.substr(1), function (err, da ...

  6. c++ 与 lua 简单交互参数介绍

    原文http://blog.csdn.net/johnice/article/details/5517431 一.第一个例子 Hello World ! #include <stdio.h> ...

  7. ruby 【rails在win7_64位操作系统安装】

    gem update --system --source http://production.s3.rubygems.org 安装截图

  8. SVN资源库报错:Could not create the view: org.tigris.subversion.subclipse.ui.repository.RepositoriesView

    解决方法: 关闭正在运行的myeclipse,然后打开myeclipse安装路径(我的安装在c盘): c:\ProgramFiles\MyEclipse\MyEclipse Professional ...

  9. boost::fucntion 用法详解

    转载自:http://blog.csdn.net/benny5609/article/details/2324474 要开始使用 Boost.Function, 就要包含头文件 "boost ...

  10. p1627 [CQOI2009]中位数

    传送门 分析 https://www.luogu.org/blog/user43145/solution-p1627 代码 #include<iostream> #include<c ...