[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 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]的更多相关文章
- 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 ...
- 【leetcode】572. Subtree of Another Tree
题目如下: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...
- 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 ...
- 572. Subtree of Another Tree
Problem statement: Given two non-empty binary trees s and t, check whether tree t has exactly the sa ...
- 572. Subtree of Another Tree 大树里包括小树
[抄题]: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...
- [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 ...
- 【easy】572. Subtree of Another Tree
判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等) 有两种方法: 方法二:递归写法 //方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树 //方法二 ...
- LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40
572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 ...
- 【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 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
随机推荐
- MySQL 闪回工具之 binlog2sql
生产上误删数据.误改数据的现象也是时常发生的现象,作为 DBA 这时候就需要出来补锅了,最开始的做法是恢复备份,然后从中找到需要的数据再进行修复,但是这个时间太长了,对于大表少数数据的修复来讲,动作太 ...
- Kafka学习-复制
复制 Kafka可以通过可配置的服务器数量复制每个主题分区的日志(可以为每个主题设置复制因子).这允许在集群中的服务器发生故障时自动故障转移到其他副本,因此在存在故障的情况下,消息仍然可用. 其他消息 ...
- TCP三次握手(建立连接)/四次挥手(关闭连接)
TCP数据包格式 顺序号(32位):用来标识从TCP源端向TCP目的端发送的数据字节流,它表示在这个报文段中的第一个数据字节的顺序号.如果将字节流看作在两个应用程序间的单向流动,则TCP用顺序号对每个 ...
- vim - manual -个人笔记
##vim配置 ###normal > 输入命令:w 写入保存 > > 粘贴 :p(向下粘贴) P(大写向上粘贴) > > 复制 :yy 复制一行 > > 删 ...
- JPA - hibernate 的各种常见用法
近期在做的项目使用的JPA,所以在此跟大家分享一下JPA的一些常见使用方法. JPA 是 JCP 组织发布的 Java EE 标准之一,因此任何声称符合 JPA 标准的框架都遵循同样的 ...
- cuda学习1-初始庐山真面目
cuda作为gpu计算中的代表,拥有着超级高的计算效率,其原因是gpu实际相当与一台超级并行机组,使用过MPI做并行计算的人们可能知道,所谓的并行计算,简单讲就是用多个U(计算单元)来完成一个U的计算 ...
- Running R jobs quickly on many machines(转)
As we demonstrated in “A gentle introduction to parallel computing in R” one of the great things abo ...
- 《HelloGitHub》第 14 期
公告 欢迎通过在 GitHub 上新建 issues 方式推荐项目,我真心希望读者可以在 HelloGItHub,找到真正的编程乐趣! <HelloGitHub>第 14 期 兴趣是最好的 ...
- 如何运行容器?- 每天5分钟玩转 Docker 容器技术(22)
上一章我们学习了如何构建 Docker 镜像,并通过镜像运行容器.本章将深入讨论容器:学习容器的各种操作,容器各种状态之间如何转换,以及实现容器的底层技术. 运行容器 docker run 是启动容器 ...
- javascript基础-对象
原理 万物皆为对象.假设将'莫德'(我)看成对象.莫德的属性有名字,性别,年龄等. 莫德的行为有吃饭,走路,睡觉等.莫德与他人的往来即对象间的交互.对象对应世界的一个实体.类,即管理对象的分类.如果莫 ...