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)的更多相关文章

  1. 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 ...

  2. 【leetcode】Same Tree(easy)

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  3. Device Tree(三):代码分析【转】

    转自:http://www.wowotech.net/linux_kenrel/dt-code-analysis.html Device Tree(三):代码分析 作者:linuxer 发布于:201 ...

  4. easyUI之Tree(树)

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <hea ...

  5. 7.9T2EASY(easy)

    EASY(easy) sol:非常经典的题,取了一次之后,把线段树上这一段变成相反数 然后再贪心取和最大的. 重复以上操作,发现最后一定有对应的解,且根据贪心过程一定 是最大的 线段树上维护区间和最大 ...

  6. leetcode 1.回文数-(easy)

    2019.7.11leetcode刷题 难度 easy 题目名称 回文数 题目摘要 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 思路 一些一定不为回文数的 ...

  7. 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 ...

  8. 2021.07.19 BZOJ2654 tree(生成树)

    2021.07.19 BZOJ2654 tree(生成树) tree - 黑暗爆炸 2654 - Virtual Judge (vjudge.net) 重点: 1.生成树的本质 2.二分 题意: 有一 ...

  9. NYOJ 221 Tree (二叉树)

    题目链接 描述 Little Valentine liked playing with binary trees very much. Her favorite game was constructi ...

随机推荐

  1. GitHub 开源的 MySQL 在线更改 Schema 工具【转】

    本文来自:https://segmentfault.com/a/1190000006158503 原文:gh-ost: GitHub's online schema migration tool fo ...

  2. [PHP] pow指数运算函数与二进制

    1.a的-2次方=(a分之一)的2次方2.-a的2次方 3次方 按照这个规则,负负得正,负正得负,正正得正,指数是偶数最终结果是正的,是奇数就是负的3.二进制转换十进制,0b开头是二进制 <?p ...

  3. Oracle day02 函数

    order by关键字作用:用于对查询结果进行排序 用法:    1.利用asc .desc对排序列进行升序或降序    2.order by后可以添加多个列(逗号分隔),当一个列的值相同时,在按第二 ...

  4. Java开发笔记(三十二)字符型与整型相互转化

    前面提到字符类型是一种新的变量类型,然而编码实践的过程中却发现,某个具体的字符值居然可以赋值给整型变量!就像下面的例子代码那样,把字符值赋给整型变量,编译器不但没报错,而且还能正常运行! // 字符允 ...

  5. jsp基础语言-jsp异常

    JSP异常 jsp页面执行时会出现两种异常,实际是javax.servlet.jsp包中的两类异常JsError和JspException. 1.JsError 在jsp文件转换成servlet文件时 ...

  6. Building QGIS from source - step by step(随笔2)

    QT安装 在Windows上安装QGIS,首先需要安装VS,VS的版本根据需要的版本下载,注意QGIS版本与VS版本对应.另外QT下载安装也需要与VS版本的安装对应.本机系统装的VS10,对应QT版本 ...

  7. 使用django 中间件在所有请求前执行功能

    django中间是一个轻级,低耦合的插件,用来改变全局的输入和输出. 一 如何使用中间件 定义中间件 注册中间件 # 这是一个中间件代码片段的说明,在各个位置的代码将在何时执行 def simple_ ...

  8. 扒一扒EOS的前世今生

    扒一扒EOS的前世今生 EOS是什么?   EOS可以认为是Enterprise Operation System的缩写,即商用的一款分布式区块链操作系统,EOS主要为了解决百万级用户的使用问题,为企 ...

  9. 【原】Java学习笔记009 - 阶段测试

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 1.需求:打印如下 ...

  10. C++调用Opencv实践中遇到的问题备忘录

    1.编写一个显示图片的项目,但显示的图片全灰色. 原因:需要在imshow()函数前加一个namedWindow()函数.https://blog.csdn.net/mao_hui_fei/artic ...