问题描述:

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.

解题思路:

关于树的题目,第一反应就是利用DFS解答,此题也不例外。

代码:

 class Solution:
def isSubtree(self, s: TreeNode, t: TreeNode) -> bool:
def dfs(a,b):
if not a or not b: #若果a,b中存在null,处理手段
return not a and not b
#以下处理时在a,b皆不为null的情况下进行讨论
if a.val==b.val and dfs(a.left,b.left) and dfs(a.right,b.right):
return True
if b is t:#当b时t的时候,判断a的左右子树分别与t是否相等
return dfs(a.left,t) or dfs(a.right,t) return False return dfs(s,t)

第4、5行代码,将各种子树为空的情形缩略到一个条件判断中,精简了代码。

第7、8行代码,判断当前树是否和t树完全相同

第9、10行代码,只有当b是t的时候才生效,意思是如果该次执行是从第二个if递归过来的,那么就不进行判断,因为该次执行判断是当前树的子树与t的子树是否相同,并非是判断t是否与当前树相同。

最后12行代码,直接返回False即可。但如果返回的是dfs(a,t),代码执行时间会缩短75%,但是我没懂为何会缩短如此之多。

Python3解leetcode Subtree of Another Tree的更多相关文章

  1. Python3解leetcode N-ary Tree Level Order Traversal

    问题描述: Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to ...

  2. Python3解leetcode Same TreeBinary Tree Level Order Traversal II

    问题描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, fro ...

  3. Python3解leetcode Same Tree

    问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary tree ...

  4. Python3解leetcode Symmetric Tree

    问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  5. Python3解leetcode Average of Levels in Binary Tree

    问题描述: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  6. Python3解leetcode Binary Tree Paths

    问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...

  7. Python3解leetcode Lowest Common Ancestor of a Binary Search Tree

    问题描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...

  8. Python3解leetcode Binary Tree PathsAdd DigitsMove Zeroes

    问题描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  9. Python3解leetcode Binary Tree PathsAdd Digits

    问题描述: Given a non-negative integer num, repeatedly add all its digits until the result has only one ...

随机推荐

  1. GenericAPIView的使用及和视图扩展类的结合使用

    GenericAPIView的使用 from rest_framework.generics import GenericAPIView GenericAPIView继承 APIView,主要增加了操 ...

  2. 002-使用Spring实现读写分离(MySQL实现主从复制)

    一. 背景 一般应用对数据库而言都是“读多写少”,也就说对数据库读取数据的压力比较大主库,负责写入数据,我们称之为:写库:从库,负责读取数据,我们称之为:读库: 1. 读库和写库的数据一致:2. 写数 ...

  3. 类LinkedHashSet

    /* * LinkedHashSet底层数据结构由哈希表和链表组成 * 哈希表保证元素的唯一性 * 链表保证元素有序(存储和取出是一致的) * */ import java.util.LinkedHa ...

  4. 机器学习--如何将NLP应用到深度学习(3)

    数据收集以后,我们下面接着要干的事情是如何将文本转换为神经网络能够识别的东西.   词向量 作为自然语言,只有被数学化才能够被计算机认识和计算.数学化的方法有很多,最简单的方法是为每个词分配一个编号, ...

  5. UVa 11582 Colossal Fibonacci Numbers! 紫书

    思路是按紫书上说的来. 参考了:https://blog.csdn.net/qwsin/article/details/51834161  的代码: #include <cstdio> # ...

  6. 史上最全的ORACLE基础教程

    ORACLE命令和语句挺多,全部记忆下来不现实,况且有不常用的指令.下面把大部分的指令做了记录和详细的注释.建议收藏.转发此篇文章,如果忘记可以翻出来查查.关注公众号it_learn获取更多学习资源 ...

  7. django shell的基本使用

    作者:python技术人 博客:https://www.cnblogs.com/lpdeboke/ 在日常工作再发中,经常需要测试一些对象.函数.类...等是否正确,但是如果整体运行项目特别麻烦,并且 ...

  8. MySQL-第十一篇JDBC典型用法

    1.JDBC常用方式      1>DriverManager:管理JDBC驱动的服务类.主要用于获取Connection.其主要包含的方法: public static synchronize ...

  9. JDK11 | 第五篇 : 启动单个Java源代码文件的程序

    文章首发于公众号<程序员果果> 地址 : https://mp.weixin.qq.com/s/h1L4FmzVSix434gVt8Fc7w 一.简介 JEP330-启动单文件代码程序(L ...

  10. [2019杭电多校第六场][hdu6638]Snowy Smile(维护区间最大子段和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意为在一个平面上任意选择一个长方形,使得长方形内点权和最大. 因为长方形可以任意选择,所以上下 ...