【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个点的一个树.这个树的边是 ...
随机推荐
- hadoop distcp 命令使用指导
1.概述 DistCp(distributed copy)是一款被用于大型集群间/集群内的复制工具. 它使用MapReduce来实现其分布,错误处理和恢复以及报告.它将文件列表和目录扩展为map任务的 ...
- vscode常用快捷键
一.vs code 的常用快捷键列表 1.注释: a) 单行注释:[ctrl+k,ctrl+c] 或 ctrl+/ b) 取消单行注释:[ctrl+k,ctrl+u] (按下ctrl不放,再按k + ...
- Linux调整日期时间
Linux日期不准确,要更改 Linux 系统整个系统范围的时区可以使用如下命令: sudo rm -f /etc/localtime sudo ln -s /usr/share/zoneinfo/A ...
- html2canvas 识别 svg 解决方案
参考: 预览地址 http://jsfiddle.net/bv16o50f/1/ html <div class="visualization"> <svg xm ...
- [BZOJ 3227] [SDOI 2008] 红黑树(tree)
Description 红黑树是一类特殊的二叉搜索树,其中每个结点被染成红色或黑色.若将二叉搜索树结点中的空指针看作是指向一个空结点,则称这类空结点为二叉搜索树的前端结点.并规定所有前端结点的高度为- ...
- python学习日记(正则表达式)
定义 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. re 模块使 Pyth ...
- P1006 传纸条 (方格取数dp)
题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个mm行nn列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运 ...
- jmeter笔记(7)--参数化--用户定义的变量
录制的脚本里面有很多的相同的数据的时候,比如服务器ip,端口号等,当更换服务器的时候,就需要手动的修改脚本里面对应的服务器ip和端口号,比较繁琐,jmeter里面有一个用户自定义变量能很好的解决这个问 ...
- css:a:visited限制
:active 对于:active伪类可以在div上生效.没有限制 :visited使用限制 :visited只适用于带href的a标签.如果给a标签绑定了click事件,那跳转的url必须跟href ...
- 查看macOS下正在使用的zsh
使用dscl . -read /Users/$USER UserShell查看 如果你的结果是/bin/zsh,又恰巧用brew安装了zsh的话,那么你可能就白安装了 将brew安装的zsh添加到/e ...