Python3解leetcode Subtree of Another Tree

问题描述:

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解答,此题也不例外。

代码:

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

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

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

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

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

原文地址:https://www.cnblogs.com/xiaohua92/p/11525521.html

时间: 2024-11-05 19:38:12

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

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, from left to right, level by level from leaf to root). For example:Given binary tree [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 return its bottom-up level

C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 这个题目是给你一棵树的中序遍历和后序遍历,让你将这棵树表示出来.其中可以假设在树中没有重复的元素. 当做完这个题之后,建议去做做第105题,跟这道题类似. 分析:这个解法的基本思想是:我们有两个数组,分别是IN和POST.后

Python3解leetcode Linked List Cycle

问题描述: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked list, we use an integer poswhich represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycl

Python3解leetcode Factorial Trailing Zeroes

问题描述: Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explanation: 3! = 6, no trailing zero. Example 2: Input: 5 Output: 1 Explanation: 5! = 120, one trailing zero. Note: Your solution should be in logari

Python3解leetcode Isomorphic Strings

问题描述: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of charac

LeetCode 572. 另一个树的子树(Subtree of Another Tree) 40

572. 另一个树的子树 572. Subtree of Another Tree 题目描述 给定两个非空二叉树 s 和 t,检验?s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 每日一算法2019/6/12Day 40LeetCode572. Subtree of Another Tree 示例 1: 给定的树 s: 3 / 4 5 / 1 2 给定的树 t: 4 / 1 2 返回 true,因为 t

Python解Leetcode: 226. Invert Binary Tree

leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): d

LeetCode: Validata Binary Search Tree

LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree o

LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal

不断递归的实现!!!! 下面是AC代码: 1 /** 2 * Given inorder and postorder traversal of a tree, construct the binary tree. 3 * @param inorder 4 * @param postorder 5 * @return 6 */ 7 public TreeNode buildTree(int[] inorder,int[] postorder){ 8 if(inorder == null || po