【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个点的一个树.这个树的边是 ...
随机推荐
- Make a Person 闭包
用下面给定的方法构造一个对象. 方法有 getFirstName(), getLastName(), getFullName(), setFirstName(first), setLastName(l ...
- python实现对文件的全量、增量备份
#!/user/bin/env python # @Time :2018/6/6 10:10 # @Author :PGIDYSQ #@File :FileBackup2.py import os i ...
- LVS实现负载均衡安装配置详解
=========实践LVS/NAT模式========== 1.实验环境 三台服务器,一台作为 director,两台作为 real server,director 有一个外网网卡(172.16.2 ...
- python操作随笔
# -*- encoding: utf-8 -*-import urllib2from bs4 import BeautifulSoupimport re f1 = open('E:/1.txt')l ...
- mysql-笔记-numberic 方法 操作符
DIV 整数除法---结果舍去小数部分, /除法 ---除以0时返回 null值 -减法 % MOD 取模 ---结果为 余数 也可以用于有小数部分的数返回余数,mod(N,0)返回null值 + 加 ...
- Laravel数据库迁移
Laravel的数据迁移功能很好用,并且可以带来一系列好处.通过几条简单的 artisan 命令,就可以顺利上手,没有复杂的地方 注意:该系列命令对数据库非常危险,请准备一个单独的数据库作为配套练习, ...
- 微信小程序工具类
wechat-common-sdk ? 场景:目前工作中的项目需要包含并使用另一个项目. 也许是第三方库,或者你独立开发的,用于多个父项目的库. 现在问题来了:你想要把它们当做两个独立的项目,同时又想 ...
- Codeforces Round #523 (Div. 2) D. TV Shows 模拟(多重集 先把所有区间加入多重集合)+贪心+二分
题意:给出n个电视节目的起始和结束时间 并且租一台电视需要x +y*(b-a) [a,b]为时段 问完整看完电视节目的最小花费是多少 思路:贪心的思想 情况1 如果新租一台电视的花费<=在空 ...
- [PL]如果天空是黑暗的,那就摸黑生存
“如果天空是黑暗的,那就摸黑生存:如果发出声音是危险的,那就保持沉默:如果自觉无力发光的,那就蜷缩于墙角.但不要习惯了黑暗,就为黑暗辩护:不要为自己的苟且而得意:不要嘲讽那些比自己更勇敢更热情的人们. ...
- bootstrap: 内联表单;
<form class="form-inline"> <div class="form-group"> <label for=&q ...