572. Subtree of Another Tree
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的更多相关文章
- 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 ...
- 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 大树里包括小树
[抄题]: Given two non-empty binary trees s and t, check whether tree t has exactly the same structure ...
- 【leetcode】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】572. 另一个树的子树 Subtree of Another Tree(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 方法二:DFS + DFS 方法三 ...
- 50. 树的子结构[subtree structure in tree]
[本文链接] http://www.cnblogs.com/hellogiser/p/subtree-structure-in-tree.html [题目] 输入两棵二叉树A和B,判断B是不是A的子结 ...
随机推荐
- nginx反向代理与负载均衡
一:nginx反向代理与负载均衡配置思路与用法 1.nginx反向代理:就是你去相亲时,媒婆就是这里的代理,让媒婆带你去见姑娘 2.nginx负载均衡:就是有很多的媒婆经过商量给你选出最适合你的姑娘, ...
- CSS3 transition 浏览器兼容性
1.兼容性 根据canius(http://caniuse.com/#search=transition),transition 兼容性如下图所示: <!DOCTYPE html> < ...
- Java异常处理总结
题记:本文为工作十年回顾总结系列之Java语言之异常处理篇,主要内容为<Thinking in Java >第四版和<Effective Java>第二版的阅读笔记,网上流传的 ...
- (转)Nginx + uwsgi + web.py + MySQLdb
一.背景知识: Web服务器上有多种方式运行Python: 传统上的CGI方式:基本已被淘汰 mod_python的模块方式:仍在广泛使用,但生产环境中也被逐步淘汰 uwsgi方式:目前生产环境下最受 ...
- js全选与反选
HTML结构: <!doctype html><html><head><meta charset="utf-8"><title ...
- 数据库dbutils
common-dbutils.jarQueryRunnerupdate方法:* int update(String sql, Object... params) --> 可执行增.删.改语句* ...
- HANA CDS与ABAP CDS
如果你在网络或者SCN上面搜索CDS,即SAP的Core Data Services,你会很容易地找到类似“Core Data Services(CDS)是一个在SAP HANA中用于定义和消费富语义 ...
- web console实现
一.效果图 二.实现 web console是基于websocket实现的. 以上做的效果嵌入项目中,因为项目本身是angular1的项目,所以console整体封装成一个angualr modul ...
- python学习随笔(一)
我是为记录学习python的过程而开通了博客,希望以后自己能看看,也希望能分享一些给初学者. 话不多说,开始第一篇. (一)python解释器. python2和3,现在python2是主流,但是py ...
- Android 布局(线性布局、相对布局)
一.线性布局(LinearLayout) <LinearLayout****</LinearLayout>1. orientation(布局方向)value=0 horizontal ...