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.

 
 

题目标签:Tree

  这道题目给了我们两个二叉树s, t,让我们判断一下,t 是不是 s 的子树。首先我们想一下,如果 s 和 t 一摸一样,那么 t 也是 s 的子树。如果s和t不一样, 那么我们要依次从 s 的 left 当作一个新的树,和 t 比较,如果不是;从 s 的 right 当作一个新的树,和 t 比较。直到把 s 树遍历结束;其中如果遇到 s 等于 t 的情况,立即返回true即可。那么我们要另外设一个function,来判断一下是否两个二叉树是相同的。

Java Solution:

Runtime beats 83.10%

完成日期:06/30/2017

关键词:Tree

关键点:recursively call trick:

  recursively - return function(left) || function(right) : 只需要两个中任何一个function call 的返回值是true,那么答案就是true,运用在遍历 s 树的每一个点 和 t 比较;

  recursively - return function(left) && function(right): 需要两个children都是返回true, 答案才是true,运用在判断两颗树否则相同。

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
public boolean isSubtree(TreeNode s, TreeNode t)
{
if(s == null)
return false; if(isSame(s,t)) // check is tree s and t are same
return true; // if tree s and t are not same, recursively call to pass s children
return isSubtree(s.left, t) || isSubtree(s.right, t);
} public boolean isSame(TreeNode s, TreeNode t)
{
if(s == null && t == null) // if two nodes are null, they are same
return true; if(s == null || t == null) // if one node is null, another is not null, they are not same
return false; if(s.val != t.val) // if two node values are not same
return false; // if two node values are same, continue to check their children until end of tree
return isSame(s.left, t.left) && isSame(s.right, t.right);
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/6828687.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 572. Subtree of Another Tree (是否是另一个树的子树)的更多相关文章

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

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

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

  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. [程序员代码面试指南]二叉树问题-判断t1树是否包含t2树的全部拓扑结构、[LeetCode]572. 另一个树的子树

    题目1 解 先序遍历树1,判断树1以每个节点为根的子树是否包含树2的拓扑结构. 时间复杂度:O(M*N) 注意区分判断总体包含关系.和判断子树是否包含树2的函数. 代码 public class Ma ...

  5. LeetCode 572. 另一个树的子树 | Python

    572. 另一个树的子树 题目来源:https://leetcode-cn.com/problems/subtree-of-another-tree 题目 给定两个非空二叉树 s 和 t,检验 s 中 ...

  6. Java实现 LeetCode 572 另一个树的子树(遍历树)

    572. 另一个树的子树 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树 ...

  7. LeetCode 572. 另一个树的子树

    题目链接:https://leetcode-cn.com/problems/subtree-of-another-tree/ 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和 ...

  8. 力扣Leetcode 572. 另一个树的子树

    另一个树的子树 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 示例 ...

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

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

随机推荐

  1. php中的多条件查询

    首先是查询所有,步骤不详述,连接数据库,查询表中的所有信息,foreach循环以表格的形式打印出来 然后就是form表单中提交查询的数据,这里以post方式提交到本页面,所以要判断post中是否有值, ...

  2. 关于args的一个小bug

    我在开始学习Java的时候就有点疑惑,到底main方法中的args到底是什么?经过我的一些思考,然后结合代码写一点自己的看法. 下面来看一段代码: /** * @author 薛定谔的猫 * 关于ar ...

  3. linux系统命令<二>----du的使用方法

    1> 要显示一个目录树及其每个子树的磁盘使用情况 du /home/linux 这在/home/linux目录及其每个子目录中显示了磁盘块数. 2> 要通过以1024字节为单位显示一个目录 ...

  4. svn服务器配置与客户端的使用

    1, Apache Subversion 官网下载地址: http://subversion.apache.org/packages.html#windows 官网下载提供的一般都是最新版本的,如果想 ...

  5. Git的使用详解

    起步 关于版本控制 Git 简史 Git 基础 安装 Git 初次运行 Git 前的配置 获取帮助 小结 Git 基础 取得项目的 Git 仓库 记录每次更新到仓库 查看提交历史 撤消操作 远程仓库的 ...

  6. append、extend与insert的区别

    最近在自学Python语言,看到向列表增加更多数据时被append(),extend(),insert()方法绕晕了. 作为编程0基础的小白,觉得有必要自己再梳理一遍: 1.append()方法是指在 ...

  7. 【重点突破】——Cookie的使用

    cookie:小甜饼 cookie:保存客户端浏览器中一个纯文本文件 版本高的浏览器可查看   F12->Resource  左下方cookie    查看 cookie作用: 保存:[安全性要 ...

  8. Suneast & Daxia (规律)

    Suneast & Daxia Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u ...

  9. bzoj4236 JOIOJI hash 模拟

    JOIOJI桑是JOI君的叔叔."JOIOJI"这个名字是由"J.O.I"三个字母各两个构成的. 最近,JOIOJI桑有了一个孩子.JOIOJI桑想让自己孩子的 ...

  10. Codeforces Round #309 (Div. 2)D

    C. Kyoya and Colored Balls time limit per test 2 seconds memory limit per test 256 megabytes input s ...