问题描述:

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. ora-01033,ORA-16038

    ORA-01033: ORACLE initialization or shutdown in progress 1.进入CMD,执行set ORACLE_SID=fbms,确保连接到正确的SID:2 ...

  2. 如何实现动态水球图 --》 echars结合echarts-liquidfill实现

    1)项目中作为项目依赖,安装到项目当中(注意必须要结合echars) npm install echarts vue-echarts --save npm install echarts-liquid ...

  3. Mac入门--通过Homebrew安装PHP(新)

    1 首先安装homebrew,安装过的话更新 安装:homebrew官网地址:https://brew.sh/index_zh-cn.html.或者直接复制下面代码: /usr/bin/ruby -e ...

  4. python+selenium元素定位——8种方法

    定位元素,selenium提供了8中元素定位方法: (1)find_element_by_id() :html规定,id在html中必须是唯一的,有点类似于身份证号 (2)find_element_b ...

  5. jmeter _Random函数生成随机数

    因对发送邮件接口做压测发现相同数据对服务器的压力很小所以需要每次发送请求都需要不同的参数,所以要对某个字段做随机数 选项中-函数助手对话框

  6. jJava第一周学习总结

    对于本学期要学习的Java程序语言,经过一周多的学习,我们首先在第一堂课了解了Java平台以及Java的历史,接着认识了JDK,初步了解了JDK的安装,JDK运行环境的设置,包括其中路径path的设置 ...

  7. idea 社区版本创建javaweb项目 使用jetty

    idea社区版本 创建javaweb项目后使用jetty启动 <dependencies> <dependency> <groupId>javax.servlet& ...

  8. 项目被os x占用

    xattr -d com.apple.FinderInfo 空格后拖入项目回车就行了

  9. C# DataSet转JSON

    经常会遇到系统数据交互采用JSON数据格式进行交互的,避免不必要的重复工作,记录下自己的处理方式. 获取数据集之后,通过函数对数据集信息进行整理通过.Net Framework3.5提出的JavaSc ...

  10. git ssh key配置&解决git每次输入密码

    git ssh key配置&解决git每次输入密码:https://blog.csdn.net/qq_42817227/article/details/81415404