Problem statement:

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 scould 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:

Two extra functions to solve this problem
void find_node() : find all nodes whose value are equal to the root of t.
bool is_subtree(): find if any start node can for a subtree which is same with t.

class Solution {
public:
bool isSubtree(TreeNode* s, TreeNode* t) {
vector<TreeNode*> node_set;
find_node(s, t, node_set);
for(auto node : node_set){
if(is_subtree(node, t)){
return true;
}
}
return false;
} private:
void find_node(TreeNode* s, TreeNode* t, vector<TreeNode*>& node_set){
if(s == NULL){
return;
}
if(s->val == t->val){
node_set.push_back(s);
}
find_node(s->left, t, node_set);
find_node(s->right, t, node_set);
return;
}
bool is_subtree(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;
}
return is_subtree(s->left, t->left) && is_subtree(s->right, t->right);
}
};

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

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

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

  4. 【leetcode】572. Subtree of Another Tree

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

  5. [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 ...

  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. SolrCloud的介绍

    SolrCloud(solr云)是Solr提供的分布式搜索方案. 当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud. 当索引量很大,搜索请求并发很高时,同样需要使用SolrClou ...

  2. Java标准注释配置

    eclipse中java文件头注释格式设置 windows->preferences->java->Code Templates->comments->Type-> ...

  3. session的使用

    一.什么是session? Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时, ...

  4. strspn 和strcspn

    1.strcspn头文件:#inclued<string.h>定义函数:size_t strcspn(const char *s, const char * reject);函数说明:st ...

  5. MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ...

    下面是我update数据库时打印出来的异常: ### Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSynt ...

  6. Open-Falcon 监控系统监控 MySQL/Redis/MongoDB 状态监控

    背景: Open-Falcon 是小米运维部开源的一款互联网企业级监控系统解决方案,具体的安装和使用说明请见官网:http://open-falcon.org/,是一款比较全的监控.而且提供各种API ...

  7. 学习Jammendo代码的心路历程(一)简单的淡出效果实现

    最近在看 Jammendo代码,打算将学习过程简单的记录下来,下面开始第一篇: 打开Jammendo运行之后,出弹出一个对话框,跳过对话框之后,会有一个淡出界面跳转到首页效果的实现.那么这个效果是怎么 ...

  8. Python之路-正则表达式

    作业一:整理正则表达式博客 正则表通常被用来检索.替换那些符合某个模式(规则)的文本,为了提取对自己有用的信息,由命令解释执行:而通配符和命令是同一级别,为了提示处理效率,直接由shell解释执行. ...

  9. Snapman设计中的思考

    Snapman主页:http://www.snapman.xyz 原文链接地址:http://www.snapman.xyz/newsitem/277785310​   feiren工作室主要研究人类 ...

  10. [编织消息框架][JAVA核心技术]动态代理应用5-javassist

    基础部份: 修改class我们用到javassist,在pom.xml添加 <properties> <javassist.version>3.18.2-GA</java ...