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. Calico 的默认连通性 - 每天5分钟玩转 Docker 容器技术(69)

    前面我们完成了 Calico 网络的部署并运行了容器,今天将讨论 Calico 的连通性. 测试一下 bbox1 与 bbox2 的连通性: ping 成功,数据包流向如下图所示. ① 根据 bbox ...

  2. python之路模块与包

    一.import加载的模块分为四个通用类别:          1 使用python编写的代码(.py文件) 2 已被编译为共享库或DLL的C或C++扩展 3 包好一组模块的包 4 使用C编写并链接到 ...

  3. Mybatis第一篇【介绍、快速入门、工作流程】

    什么是MyBatis MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为 ...

  4. JDBC数据库之添加数据

    通过JDBC向数据库中添加数据,可以使用INSERT语句实现插入数据SQL语句,对于SQL语句中的参数可以只用占位符"?"代替,然后通过PreparedStatement对其赋值以 ...

  5. Python装饰器主要用法

    #!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = '人生入戏' user = "admin" passwd = ...

  6. 14.LINUX-platform机制实现驱动层分离(详解)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 本节目标:        学习platform机制,如何实现驱动层分离 1.先来看看我们之前分析输入子系统的分层概念,如下图所示: 如上图所示,分 ...

  7. Ansible系列(七):执行过程分析、异步模式和速度优化

    本文目录:1.1 ansible执行过程分析1.2 ansible并发和异步1.3 ansible的-t选项妙用1.4 优化ansible速度 1.4.1 设置ansible开启ssh长连接 1.4. ...

  8. 用postal.js在AngularJS中实现订阅发布消息

    点击查看AngularJS系列目录 用postal.js在AngularJS中实现event bus 用postal.js在AngularJS中实现event bus 理想状态下,在一个Angular ...

  9. MMORPG战斗系统随笔(二)、浅谈场寻路Flow Field PathFinding算法

    转载请标明出处http://www.cnblogs.com/zblade/ 今天给大家带来一篇游戏中寻路算法的博客.去年,我加入一款RTS的游戏项目,负责开发其中的战斗系统,战斗系统的相关知识,属于游 ...

  10. 分享ES6中比较常用又强大的新特性

    前言 es6有很多新东西,但是感觉常用的并不是很多,这里学习记录了一些我自己认为非常常用又强大的新特性. scoping 实用的块级作用域,let x = xxx 可以声明一个块级作用域的局部变量,简 ...