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

示例 1:

给定的树 s:

3 / \ 4 5 / \ 1 2

给定的树 t:

4 / \ 1 2

返回 true,因为 t 与 s 的一个子树拥有相同的结构和节点值。

示例 2:

给定的树 s:

3 / \ 4 5 / \ 1 2 / 0

给定的树 t:

4 / \ 1 2

返回 false。

class Solution {
public:
bool isSubtree(TreeNode* s, TreeNode* t)
{
if(s == NULL)
return false;
if(s ->val == t ->val)
if(isSame(s, t))
return true;
return isSubtree(s ->left, t) || isSubtree(s ->right, t);
} bool isSame(TreeNode* s, TreeNode* t)
{
if(s == NULL && t == NULL)
return true;
else if(s != NULL && t != NULL)
{
if(s ->val != t ->val)
return false;
return isSame(s ->left, t ->left) && isSame(s ->right, t ->right);
}
else
return false;
}
};

Leetcode572.Subtree of Another Tree另一个树的子树的更多相关文章

  1. 第26题:LeetCode572:Subtree of Another Tree另一个树的子树

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

  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. LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40

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

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

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

  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. 另一个树的子树

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

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

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

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

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

随机推荐

  1. date函数

  2. iOS开发本地推送(iOS10)UNUserNotificationCenter

    1.简介 iOS10之后苹果对推送进行了封装,UNUserNotificationCenter就这样产生了.简单介绍本地推送的使用UserNotifications官方文档说明! 2.简单使用UNUs ...

  3. PAT甲级——A1070 Mooncake

    Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types ...

  4. Mybatis和spingboot整合

    0. 导包 <!-- 统一管理springboot相关的包 --> <parent> <groupId>org.springframework.boot</g ...

  5. RQNOJ--160 竞赛真理(01背包)

    题目http://www.rqnoj.cn/problem/160 分析:这是一个01背包问题,对于每一道题目,都有两个选择"做"或者"不做". 但是唯一不同的 ...

  6. JS倒计时两种种实现方式 很不错

    最近做浏览器界面倒计时,用js就实现,两种方式: 一:设置时长,进行倒计时.比如考试时间等等 代码如下: <html> <head> <meta charset=&quo ...

  7. jqGrid的subGrid子表格

    使用完整jqGrid作为子表格 使用子表格,涉及到jqGrid的三个选项: subGrid :首先必须将jqGrid的subGrid选项设置为true,默认为false:当此项设为true的时候,Gr ...

  8. 打开springboot的run dashboard

    默认情况下,idea的run dashboard是关闭的,当检测到你有多个springboot项目时会弹出提示框,询问是否打开. 如果我们想要自己打开,需要修改配置. 在你的idea的项目目录中,有一 ...

  9. linux平台进行c语言源码安装

    安装c源程序的步骤: 1. ./configure --prefix 执行编译检测 指定安装路径 2. make 编译 3. sudo make install 编译后安装 前两步可以合成一步(mak ...

  10. 矩阵快速幂2 3*n铺方格

    #include <iostream> #include <cstdlib> #include <cstring> #include <queue> # ...