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 ...
随机推荐
- .Net File类的操作
//File类的常用操作方法 //File类的常用操作方法 //File.Exists //检测是否存在该文件 Exists //File.Create //创建一个文件 Create //File. ...
- year 和 weak year 的区别
java 中使用 SimpleDateFormat 时,会遇到 year 和 week year 这两个概念,特此记录. google 答案: A week year is in sync with ...
- Linux设置Swap虚拟内存方法
linux可以文件或者分区来当作虚拟内存. 首先查看当前的内存和swap 空间大小(默认单位为k, -m 单位为M): free -m 查看swap信息,包括文件和分区的详细信息 swapon -s或 ...
- 20190322-a标签、img标签、三列表、特殊字符实体、表格
目录 1.a标签 a标签的属性 锚点 2.img标签 img标签的属性 图像热区 3.三列表 有序列表(Ordered List) ol>li 无序列表(Unordered List) ...
- ifram 局部刷新,不刷新父级
function loadIframe(url) { //获取url链接 var u = window.location.href; //因为每次获取的链接中都有之前的旧锚点, //所以需要把#之后的 ...
- 【20190304】JavaScript-知识点总结:Set,异或
ES6新特性:Set ES6提供了新的数据结构Set,Set对象不是数组, 可以用来保存对象或者基本类型, 所有保存的值都是唯一的, chrome浏览器>38和FF>13,以及nodeJS ...
- angular打包后路由和文件路径不对
base href换成如下script标签 <!-- <base href="/"> --> <script> document.write(' ...
- arcgis api 3.x for js 入门开发系列十二地图打印GP服务(附源码下载)
前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...
- 超级强大的socket工具ss,替代netstat
1.结论:ss 命令比netstat 更强大,提供功能更多,并且性能更高. 2.显示当前系统的socket占用总体宏观情况. ss -s 当已创建的socket数过多时,已经说明系统配置存在问题. 3 ...
- FFmpeg AVPacket相关主要函数介绍
1.AVPacket相关函数介绍 操作AVPacket的函数大约有30个,主要分为:AVPacket的创建初始化,AVPacket中的data数据管理(clone,free,copy),AVPacke ...