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. python day2 练习题

    #/usr/bin/env python # -*- coding:utf-8 -*- # Author:ylw # name = '  ylwA  ' # 1.移除name变量对应的值两边的空格 # ...

  2. Gephi安装

    Gephi for mac https://gephi.org/users/download/ 在官网上下载gephi-0.9.1-macos.dmg双击拖到Application里面就好了,注意有的 ...

  3. 如何用PHP遍历文件数目 或删除目录下的全部文件?

    先说一下基础知识: 文件位置如下图所示: 1.判断是文件还是目录 var_dump(filetype("./aa/bb/cc.txt")); 输出: string(4) " ...

  4. EntityFramework6.X 之LocalDB&ConnectionString

    LocalDB 面向开发人员的SQL Server Express的执行模式,它的安装将复制启动SQL Server数据库引擎所需的最少文件集且使用特定连接字符串来启动连接,它是可以创建和打开SQL ...

  5. TensorFlowSharp入门使用C#编写TensorFlow人工智能应用

    TensorFlowSharp入门使用C#编写TensorFlow人工智能应用学习. TensorFlow简单介绍 TensorFlow 是谷歌的第二代机器学习系统,按照谷歌所说,在某些基准测试中,T ...

  6. 让xcode8支持7.0的设备

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

  7. 利刃 MVVMLight

    已经很久没有写系列文章了,上一次是2012年写的HTLM5系列,想想我们应该是较早一批使用HTML5做项目的人. 相比我当时动不动100+的粉丝增长和两天3000+的阅读量,MVVM Light只能算 ...

  8. CodeForces 544C (Writing Code)(dp,完全背包)

    题意:有n个程序员,要协作写完m行代码,最多出现b个bug,第i个程序员每写一行代码就会产生a[i]个bug,现在问,这n个人合作来写完这m行代码,有几种方案使得出的bug总数不超过b(题中要求总方案 ...

  9. javascript基础-性能优化

    优化点 性能检测 基调网络 http://www.cesule.com/cesule/status/show/3496d91653a14743af2bd2e261aee204 阿里测 http://a ...

  10. centos rabbitmq 安装

    MQ 的一个产品[消息队列] rabbitmq 的本质<1>rabbitmq 是用什么语言编写的? => erlang<2>rabbitmq 其实是遵循amqp 协议的一 ...