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相等即可。

bool isSameTree(TreeNode* s, TreeNode* t)
{
if(s == NULL && t== NULL) return true;
if( (s == NULL && t != NULL )|| (s != NULL&&t == NULL) || (s->val !=t->val)) return false;
bool left = isSameTree(s->left,t->left);
bool right = isSameTree(s->right,t->right);
return (left && right);
}
bool isSubtree(TreeNode* s, TreeNode* t)
{
bool res = false;
if(s!=NULL && t!= NULL)
{
if(t->val== s->val) res = isSameTree(s,t);
if(!res) res = isSubtree(s->left,t);
if(!res) res = isSubtree(s->right,t);
}
return res;
}

[leetcode-572-Subtree of Another Tree]的更多相关文章

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

  2. 【leetcode】572. Subtree of Another Tree

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

  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. 572. Subtree of Another Tree

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

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

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

  6. [LC] 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 ...

  7. 【easy】572. Subtree of Another Tree

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

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

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

  9. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  10. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

随机推荐

  1. 开启MongoDB客户端访问控制

    参考官方文档:https://docs.mongodb.org/v2.6/tutorial/enable-authentication/ 基于版本:MongoDB 2.6 概览 在MongoDB数据实 ...

  2. 腾讯IVWEB团队:前端 fetch 通信

    欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者:villainthr 文章摘自: 前端小吉米 随着前端异步的发展, XHR 这种耦合方式的书写不利于前端 ...

  3. 通过js给网页加上水印背景

    有些后端管理系统,因为业务逻辑的需要,需要加上水印,下面就是水印方法. function watermark(settings) { debugger; //默认设置 var defaultSetti ...

  4. 让xcode8支持7.0的设备

    升级到xcode8之后发现不能支持7.0设备 1 . 下载文件将文件覆盖到 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS. ...

  5. [原创]MySQL数据库忘记root密码解决办法

    MySQL数据库忘记root密码解决办法 1.在运行输入services.msc打开服务窗体,找到MYSQL服务.右键停止将其关闭.如图:

  6. springboot thymeleaf和shiro标签整合

    这里用的是 thymeleaf 2.x版本的 添加依赖 <dependency> <groupId>com.github.theborakompanioni</group ...

  7. mysql5.6 主从复制

    Master 192.168.59.128 Slave 192.168.59.129   默认认为已安装mysql5.6  mysql5.6 rpm安装配置 修改Master my.cnf文件   # ...

  8. 细说"回车"和"换行"的故事

    引言 最近在php还有c#以及memcache的shell当中经常看到\r\n的写法,刚开始还没注意, 不过后面感觉这样写有些不对头,\r表示回车  \n表示换行,那这样不是换行了两次吗? 为了解决疑 ...

  9. 安装lnmp集成环境

    具体配置看原文,不重新复述: 原文:https://lnmp.org/install.html 因为配置数据库主从,需要保持两台mysql数据库服务器的mysql版本号一致,所以又重新装了一次..重新 ...

  10. kotlin成长之路

    前言: 从接触Kotlin开始,也就是我今天开启写技术博客的决定,文采不佳,欢迎各位阅读者的理解与指点.而该篇文章是最为博客新手的我对Kotlin成长的引导篇,所以内容一般是Kotlin技术博客的目录 ...