当前位置:网站首页>Sword finger offer 26 Substructure of tree
Sword finger offer 26 Substructure of tree
2022-07-18 13:35:00 【The harder you work, the luckier you are】
The essence of recursion is stack .
Recursion requires a termination condition
The traversal of binary tree mainly includes four ways , Sequence traversal 、 The former sequence traversal 、 In the sequence traversal 、 After the sequence traversal . Sequence traversal is usually called breadth first search BFS, Usually with the help of the first in first out feature of the queue , Traverse the tree layer by layer
front 、 in 、 Post order traversal belongs to depth first search , That is to say Depth FIrst Search abbreviation DFS, The processing methods of depth first search can be generally divided into recursive and non recursive methods , The non recursive way is generally stack, which is different from the breadth first queue . For the former 、 in 、 The division of post order is actually to distinguish the order of access nodes :
The former sequence traversal : The root node –> The left subtree –> Right subtree
In the sequence traversal : The left subtree –> The root node –> Right subtree
After the sequence traversal : The left subtree –> Right subtree –> The root node
Ideas
Binary trees generally use : recursive 、 Traversal to solve
The problem is divided into two steps :
First step , In the tree A And trees in the forest B The value of the root node is the same as that of the node R;
The second step , Judgment tree A China and Israel R Whether the subtree of the root node contains and trees B Same structure .
Both steps can be implemented using recursion
Recursion needs to pay attention to the end condition
In the second step ,
If node R Values and trees for B The root node of is different , with R Subtrees and trees that are root nodes B Must not have the same node ;
If they have the same value , Then recursively judge whether the values of their respective left and right nodes are the same .
The termination condition of recursion is that we reach the tree A Or trees B Leaf node of .
Reference blog
class Solution:
def isSubStructure(self, A, B):
if (not A) or (not B):#A Empty or B empty
return False
def contain(A,B):# Judge B Whether it is A Substructure of
if not B: return True#B empty
if not A or A.val!=B.val:return False#B Not empty ,A Empty or , Node values are different
return contain(A.left,B.left) and contain(A.right,B.right)
return contain(A,B) or self.isSubStructure(A.left,B) or self.isSubStructure(A.right,B)
边栏推荐
- Sword finger offer 22 The penultimate node in the linked list
- Time consuming evaluation of image pixel values accessed by opencv, emgucv and opencvsharp pointers (with source code)
- 软件测试月薪28K大厂面试题 (经面试官允许可以拿走试卷)
- "Harmonyos" explore harmonyos applications
- Expert routine of BW conversion based on AMDP
- EXCEL如何生成不重复的随机数 ,多方法+原理
- [software testing] test slang, the advice given to you by ten-year old employees
- DzzOffice_ Flowplayer player changes
- [programming training 4] calculate candy + hexadecimal conversion
- [machine learning] random forest
猜你喜欢

Bluetooth technology | the first Bluetooth Le audio compatible devices will be launched this year, and the endurance of Bluetooth headsets will be improved by leaps and bounds

EXCEL如何生成不重复的随机数 ,多方法+原理

Some points for attention in drawing excel charts

Dls-42/4-4 dc110v double position relay

剑指 Offer 26. 树的子结构

How to choose a desktop multimeter?

Static routing technology

动态炫酷的404页面源码

内网部署EDUSOHO遇到的问题

Expert routine of BW conversion based on AMDP
随机推荐
Brush questions in summer vacation
Dpr-34, AC220V double position relay
剑指 Offer 50. 第一个只出现一次的字符
Excel, how to choose the right chart?
剑指 Offer 27. 二叉树的镜像
ClickHouse(04)如何搭建ClickHouse集群
软件测试零基础测试篇-基本概念
Some points for attention in drawing excel charts
Aomei easy clone system disk backup
Gan online learning notes
静态路由技术
[deep learning] environment configuration of hands-on learning and deep learning
How to solve pycharm's inability to input Chinese:
Bluetooth technology | the first Bluetooth Le audio compatible devices will be launched this year, and the endurance of Bluetooth headsets will be improved by leaps and bounds
u盘删除的文件怎么恢复
Static routing technology
DzzOffice_ Flowplayer player changes
[TinyML]APQ:Joint Search for Network Architecture, Pruning and Quantization Policy
Luogu_ P3383 [template] linear sieve prime number_ Euclidean sieve prime number
Detailed explanation of some functions with similar functions in MySQL