当前位置:网站首页>Sword finger offer 26 Substructure of tree
Sword finger offer 26 Substructure of tree
2022-07-02 17:12:00 【anieoo】
Original link : The finger of the sword Offer 26. The substructure of a tree

solution:
The code is mainly divided into two steps :
① adopt dfs Judge B Yes no A The subtree of the root node
② adopt isSubstructure Function for each A Traverse the nodes in
class Solution {
public:
bool isSubStructure(TreeNode* A, TreeNode* B) {
if(B == NULL || A == NULL) return false;
return dfs(A, B) || isSubStructure(A->left, B) || isSubStructure(A->right, B);
}
bool dfs(TreeNode *A, TreeNode *B) {
if(B == NULL)
return true;
if(A == NULL || A->val != B->val)
return false;
return dfs(A->left, B->left) && dfs(A->right, B->right);
}
};边栏推荐
猜你喜欢

社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东

移动应用性能工具探索之路

How openharmony starts FA of remote devices

Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion

【Leetcode】13. 罗马数字转整数

七张图,学会做有价值的经营分析

The computer comes with software to make the background color of the picture transparent (matting white background)

Geoserver: publishing PostGIS data sources

电脑自带软件使图片底色变为透明(抠图白底)

Penetration tool - intranet permission maintenance -cobalt strike
随机推荐
PWM breathing lamp
远程办公对我们的各方面影响心得 | 社区征文
Lampe respiratoire PWM
有赞和腾讯云、阿里云一同摘得“中国企业云科技服务商50强”[通俗易懂]
What is the difference between JSP and servlet?
pwm呼吸燈
VMware install win10 image
入行数字IC验证后会做些什么?
酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
2322. Remove the minimum fraction of edges from the tree (XOR and & Simulation)
LeetCode 5. Longest Palindromic Substring
二、mock平台的扩展
使用知行之桥的API端口,提供资源供合作伙伴访问
绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
Geoserver: publishing PostGIS data sources
[error record] error -32000 received from application: there are no running service protocol
DGraph: 大规模动态图数据集
Deep learning image data automatic annotation [easy to understand]