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 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.
思路: 本质上还是树的遍历,考虑细节,递归中有递归。
/*
可以结合Leetcode 100 same tree 那道题来理解,这里要求是求一个树的子树是否包含另一个树,所以要遍历一遍第一个树(这里用先序遍历)。
所以递归函数中的函数也是递归函数,很有意思。 */
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool sameTree(TreeNode * s, TreeNode * t){
if (s == NULL && t == NULL){
return true;
}
if (s == NULL || t == NULL){
return false;
}
return s -> val == t -> val && sameTree(s -> left, t -> left) && sameTree(s -> right, t -> right);
}
bool isSubtree(TreeNode* s, TreeNode* t) {
if (s == NULL){ // 若是第一个树到了末尾还没有找到则就是返回false
return NULL;
}
if (sameTree(s , t)){
return true;
}
return isSubtree(s -> left, t) || isSubtree(s -> right, t);
}
};
100. Same Tree
Given two binary trees, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
Example 1:
Input: 1 1
/ \ / \
2 3 2 3 [1,2,3], [1,2,3] Output: true
Example 2:
Input: 1 1
/ \
2 2 [1,2], [1,null,2] Output: false
Example 3:
Input: 1 1
/ \ / \
2 1 1 2 [1,2,1], [1,1,2] Output: false
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if (p == NULL && q == NULL){
return true;
}
if (p == NULL || q == NULL){
return false;
}
return p -> val == q -> val && isSameTree(p -> left, q -> left) && isSameTree(p -> right, q -> right);
}
};
572. Subtree of Another Tree(easy)的更多相关文章
- 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】Same Tree(easy)
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- Device Tree(三):代码分析【转】
转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...
- easyUI之Tree(树)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...
- 7.9T2EASY(easy)
EASY(easy) sol:非常经典的题,取了一次之后,把线段树上这一段变成相反数 然后再贪心取和最大的. 重复以上操作,发现最后一定有对应的解,且根据贪心过程一定 是最大的 线段树上维护区间和最大 ...
- leetcode 1.回文数-(easy)
2019.7.11leetcode刷题 难度 easy 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...
- LeetCode--LinkedList--141.Linked List Cycle(Easy)
141. Linked List Cycle(Easy)2019.7.10 题目地址https://leetcode.com/problems/linked-list-cycle/ Given a l ...
- 2021.07.19 BZOJ2654 tree(生成树)
2021.07.19 BZOJ2654 tree(生成树) tree - 黑暗爆炸 2654 - Virtual Judge (vjudge.net) 重点: 1.生成树的本质 2.二分 题意: 有一 ...
- NYOJ 221 Tree (二叉树)
题目链接 描述 Little Valentine liked playing with binary trees very much. Her favorite game was constructi ...
随机推荐
- Ubuntu中安装 mercurial – TortoiseHG
sudo add-apt-repository ppa:tortoisehg-ppa/releases sudo add-apt-repository ppa:mercurial-ppa/releas ...
- Redis in python
什么是Redis 数据库类型分为两种,关系型和非关系型,Redis是一个非常重要的非关系型数据库. 既然是数据库,就是存储数据的一个空间,或者说是一个软件,非关系就是不再按照一对一多对多等结构进行外键 ...
- [PHP] assert()断言检测函数
assert_options函数 设置断言的参数 assert 函数 ,检测一个断言 <?php // 激活断言,并设置它为 quiet assert_options(ASSERT_ACTIVE ...
- 【Javaweb】poi实现通过上传excel表格批量导入数据到数据库
1.导入poi相关jar包 对于只操作2003及以前版本的excel,只需要导入poi-XXX.jar ,如果还需要对2007及以后版本进行操作,则需要导入 poi-ooxml-XXX.jar poi ...
- javascript常用的41个经典技巧
1. 将彻底屏蔽鼠标右键 <table border oncontextmenu=return(false)><td>no</table> 可用于Table 2. ...
- input 图片上传,第二次上传同一张图片失效
<input type="file" onchange="angular.element(this).scope().addPhoto(this,event)&qu ...
- gulp前端自动化构建并上传oss
前言 前端自动化构建工具从最开始的grunt, gulp, fis等到现在比较流行的webpack可谓层出不穷,个人还是比较倾向于gulp,虽然有的时候会因为某个插件的配置问题头疼很久,但不可否认gu ...
- 后端开发者的Vue学习之路(四)
目录 上节内容回顾: npm 介绍 安装 常用命令: 补充: 基于npm的Hello World 项目结构分析 用法迁移 小提醒 ES6语法 知识补充 单文件组件 使用注意: 路由 开启路由 定义路由 ...
- Ubuntu 16.04安装Zabbix 3.2 版本
系统环境:ubuntu16.04 注意:为了便于实验测试,需要关闭防火墙: parallels@zabbix-server:~$ sudo systemctl stop ufw parallels ...
- 美团技术沙龙01 - 58到家服务的订单调度&数据分析技术
1. 2015.4.15 到家服务的订单调度&数据分析技术 58到家· 黄海斌 @xemoaya 2.agenda • 58到家介绍 • 订单管理系统介绍 • 数据分析技术的应用 3.2015 ...