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.

Solution:

Time: O(M *N)

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null) {
return false;
}
if (isSameTree(s, t)) {
return true;
}
return isSubtree(s.left, t) || isSubtree(s.right, t);
} private boolean isSameTree(TreeNode s, TreeNode t) {
if (s == null && t == null) {
return true;
}
if (s == null || t == null) {
return false;
}
if (s.val != t.val) {
return false;
}
return isSameTree(s.left, t.left) && isSameTree(s.right, t.right);
}
}

[LC] 572. Subtree of Another Tree的更多相关文章

  1. 572. Subtree of Another Tree 大树里包括小树

    [抄题]: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...

  2. 572. Subtree of Another Tree

    Problem statement: Given two non-empty binary trees s and t, check whether tree t has exactly the sa ...

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

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

  5. 【leetcode】572. Subtree of Another Tree

    题目如下: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...

  6. 【easy】572. Subtree of Another Tree

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

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

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

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

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

  9. 50. 树的子结构[subtree structure in tree]

    [本文链接] http://www.cnblogs.com/hellogiser/p/subtree-structure-in-tree.html [题目] 输入两棵二叉树A和B,判断B是不是A的子结 ...

随机推荐

  1. dll hook 共享内存数据

    unit UnitDll; interface uses Windows; * ; // 文件映射到内存的大小 const HOOK_MEM_FILENAME = 'MEM_FILE'; // 映像文 ...

  2. mybatis-地区三表生成地区树

    package com.dhht.manager.vo.area; import lombok.Data; import java.io.Serializable;import java.util.L ...

  3. 1)warning LNK4233

    名称 test.exe 包含非 ASCII 字符,在具有除 936 以外的 ANSI 代码页的系统上可能不能加载 DLL 名称 练习动态库.dll 包含非 ASCII 字符,如果系统没有与用于链接此 ...

  4. Microsoft SQL Server Management Studio连接后报“ viewInfo (Microsoft.SqlServer.Management.SqlStudio.Expl”

    解决办法: 在路径:C:\Users\你的用户名\AppData\Local\Temp\”新建文件夹并命名为2,如果已经有 2 则看清楚是否是文件而不是文件夹,删掉文件改为文件夹: 如果是找不到\Us ...

  5. 面试准备 HTTP协议

    http协议的主要特点 简单快速  //某个资源是固定的 (统一资源符)UII 灵活  //http头部有个数据类型,完成不同数据类型的传输 无连接  //链接一次就会断开 无状态 //客户端和服务端 ...

  6. Java中常用的API(四)——其他

    前面说三篇文章分别介绍了Object.String.字符缓冲类的API,接下来我们简要介绍一下其他常用的API. 1.System System类用于获取各种系统信息,最为常用的是: System.o ...

  7. 启动zookeeper却没有进程

    第一次: 没有jdk,安装好jdk就可以了 第二次: java的环境变量没配好,按照下图的配就行: Java_HOME和jre_HOME都是jdk的目录就行 最后两行不加试试,好像都没多大关系 应该是 ...

  8. UML-类图-关联

  9. mybatis自动扫描的时候,接口跟xml文件的名字最好能够一一对应

    事实证明这是十分有好处的,当然,即便你不这么做,它也不一定会报invalid bound statement (not found),因为你不知道从哪儿拷来的配置文件可能从其他的地方做了配置,但是这么 ...

  10. 了解Kafka生产者

    了解Kafka生产者 ​ 之前对kafka的整体架构有浅显的了解,这次正好有时间,准备深入了解一下kafka,首先先从数据的生产者开始吧. 生产者的整体架构 ​ 可以看到整个生产者进程主要由两个线程进 ...