问题描述:

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. 内置函数zip,map,even

    内置函数的补充:1.zip:l1 = ['a','b','c','e','f','g']l2 = [1,2,3]l3=['A','B','C']L4=['牛','牛','niu']#zip,就是把俩l ...

  2. 用python进行月份加减的函数

    import math def add_month(datamonth, num): """ 月份加减函数,返回字符串类型 :param datamonth: 时间(20 ...

  3. 安装gradle和配置

    1:官网下载地址:https://docs.gradle.org/current/userguide/installation.html 下载自己认为的版本(压缩包) 2:解压到目标目录 3:配置gr ...

  4. oracle--单行函数和多行函数

    单行函数 1.字符函数 函  数 功  能 示  例 结 果 INITCAP (char) 首字母大写 initcap ('hello') Hello LOWER (char) 转换为小写 lower ...

  5. python基础-9.2 单例模式

    设计模式 一.单例模式 单例,顾名思义单个实例.创建一个实例 链接池案例 1.单例=>只有一个实例 2.静态方法+静态字段 3.所有的实例中封装的内容相同时用单例模式 class Connect ...

  6. 小白学Python(14)——pyecharts 绘制K线图 Kline/Candlestick

    Kline-基本示例 from pyecharts import options as opts from pyecharts.charts import Kline data = [ [2320.2 ...

  7. SQLServer查看及设置最大连接数

    很多时候自己本地开发会遇到 ,打开几个连接正常访问 之后就报错误,这时候需要调整sqlserver 最大连接数. 1. 查询最大连接数 SELECT value_in_useFROM sys.conf ...

  8. P3724 [AH2017/HNOI2017]大佬

    传送门 发现保持自信和做其他事情互不干扰,可以直接做一次 $dp$ 求出最多能空出几天来怼大佬 然后就变成给你若干天,是否能怼死大佬,考虑求出所有的 天数和输出的嘲讽值集合,因为天数不多,嘲讽值增长很 ...

  9. Lambda创建表达式目录树

    一,如下代码: using System; using System.Linq.Expressions; namespace Demo { class Program { static void Ma ...

  10. python基础面试题:(1)

    1.以下用C语言开发的Python解释器是( ) A:python是Java语言开发的Python解析器,B:PyPy是使用Python语言开发的Python解析,C:IronPython是.net平 ...