【easy】572. Subtree of Another Tree
判断一棵树中是否包含另一棵子树(包含是,两棵树重合处的根节点之下的子节点都相等)
有两种方法:
方法二:递归写法
//方法一:可以借鉴之前序列化的题目,如果序列化得到的序列一样就是相同的树
//方法二:用递归来写十分的简洁,我们先从s的根结点开始,跟t比较,如果两棵树完全相同,那么返回true,否则就分别对s的左子结点和右子结点调用递归再次来判断是否相同,只要有一个返回true了,就表示可以找得到。 class Solution {
public:
bool isSubtree(TreeNode* s, TreeNode* t) {
//方法二的递归
if (!s)
return false;
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;
if (s == NULL || t == NULL)
return false;
if (s->val != t->val)
return false;
if (s->val == t->val)
return isSame(s->left,t->left) && isSame(s->right,t->right);
}
};
方法一:比较两个字符串
//写成两个序列,判断一个序列是否包含另一个序列为子序列
class Solution {
public:
bool isSubtree(TreeNode* s, TreeNode* t) {
ostringstream os1, os2;
serialize(s, os1);
serialize(t, os2);
return os1.str().find(os2.str()) != string::npos;
}
void serialize(TreeNode* node, ostringstream& os) {
if (!node) os << ",#";
else {
os << "," << node->val;
serialize(node->left, os);
serialize(node->right, os);
}
}
};
https://www.cnblogs.com/zfyouxi/p/4074592.html
介绍ostringstream.
【easy】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 ...
- 【Leetcode】【Easy】Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【Leetcode】【Easy】Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- 344. Reverse String【easy】
344. Reverse String[easy] Write a function that takes a string as input and returns the string rever ...
- 【BZOJ1803】Spoj1487 Query on a tree III 主席树+DFS序
[BZOJ1803]Spoj1487 Query on a tree III Description You are given a node-labeled rooted tree with n n ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【BZOJ2843】极地旅行社(Link-Cut Tree)
[BZOJ2843]极地旅行社(Link-Cut Tree) 题面 BZOJ 题解 \(LCT\)模板题呀 没什么好说的了.. #include<iostream> #include< ...
- 【BZOJ4530】大融合(Link-Cut Tree)
[BZOJ4530]大融合(Link-Cut Tree) 题面 讨厌权限题!!! Loj链接 题目描述 小强要在 N个孤立的星球上建立起一套通信系统.这套通信系统就是连接 N个点的一个树.这个树的边是 ...
随机推荐
- RabbitMQ的一些有用教程
最近学习了一些RabbitMQ的知识,现在对所阅读过的一些非常优秀的教程进行总结,感谢各位博主和大神的无私奉献. 一.原理篇 https://blog.51cto.com/lookingdream/2 ...
- git和github的学习
摘要:Git是个实用而流行的工具,我在网上找了很多教程,发现很多扯来扯去的,难消化,难吸收,而廖雪峰老师的这个教程最好,由浅入深,一步一步跟着做,记录巩固下.原作网址:https://www.liao ...
- redis session 共享 测试案列
下载 spring redis session demo 2.分别在不同的服务器上启动 3.nginx 安装 测试
- Windows elasticsearch1.5.1安装
http.cors.enabled: true http.cors.allow-origin: /.*/ network.host: 192.168.2.200 http.port: cluster. ...
- mysql-笔记 隔离级别、事务
1 隔离级别:低级别的隔离通常可以执行更高的并发,系统 开销也更低 2 Read uncommitted:事务可以读取未提交的数据,脏读,应少用 3 read committed:不可重复读,事务只能 ...
- Manual write code to record error log in .net by Global.asax
完整的Glabal.asax代码: <%@ Application Language="C#" %> <script RunAt="server&quo ...
- 修改MAC地址的方法 破解MAC地址绑定(抄)
修改MAC地址的方法 破解MAC地址绑定 网卡的MAC地址是固化在网上EPROM中的物理地址,是一块网卡的“身份证”,通常为48位.在平常的应用中,有很多方面与MAC地址相关,如有些软件是和MAC ...
- js 替换所有指定的字符串
js 的replace方法只替换第一个匹配到的的字符 如果要全局替换,使用以下方法(g为全局标志) str.replace(/需要替换的字符串/g,"新字符串") //如果有特殊符 ...
- 学习STM32F769DK-OTA例程之百度云平台建立MQTT服务器
@2019-04-17 [小记] 百度云平台建立MQTT服务器时需要设置权限组,否则连接失败
- nuxt npm run dev 报错Solution to the "Error: listen EADDRINUSE 127.0.0.1:8080"
Solution to the "Error: listen EADDRINUSE 127.0.0.1:8080" Hello, Just sharing a solution t ...