当前位置:网站首页>剑指 Offer 26. 树的子结构
剑指 Offer 26. 树的子结构
2022-07-02 14:21:00 【anieoo】
原题链接:剑指 Offer 26. 树的子结构
solution:
代码主要分为两步:
① 通过dfs判断B是否是以A为根节点的子树
② 通过isSubstructure函数对每个A中的节点进行遍历
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);
}
};
边栏推荐
- 畅玩集团冲刺港股:年营收2.89亿 刘辉有53.46%投票权
- Lampe respiratoire PWM
- PWM控制舵机
- <四> H264解码输出yuv文件
- 寒门再出贵子:江西穷县考出了省状元,做对了什么?
- 配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
- Error when uploading code to remote warehouse: remote origin already exists
- 【Leetcode】14. 最长公共前缀
- Résumé de l'entrevue de Dachang Daquan
- john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
猜你喜欢
[cloud native] briefly talk about the understanding of flume, a massive data collection component
PWM控制舵机
⌈ 2022 ⌋ how to use webp gracefully in projects
寒门再出贵子:江西穷县考出了省状元,做对了什么?
Dgraph: large scale dynamic graph dataset
畅玩集团冲刺港股:年营收2.89亿 刘辉有53.46%投票权
Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
易语言abcd排序
小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
随机推荐
Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
Privacy computing technology innovation and industry practice seminar: Learning
Masa framework - DDD design (1)
【云原生】简单谈谈海量数据采集组件Flume的理解
Global and Chinese markets of stainless steel surgical suture 2022-2028: Research Report on technology, participants, trends, market size and share
Ap和F107数据来源及处理
C语言中sprintf()函数的用法
System Verilog实现优先级仲裁器
Penetration tool - intranet permission maintenance -cobalt strike
LeetCode 1. Sum of two numbers
GeoServer:发布PostGIS数据源
一文看懂:数据指标体系的4大类型
大厂面试总结大全
Go zero micro service practical series (VIII. How to handle tens of thousands of order requests per second)
如何与博格华纳BorgWarner通过EDI传输业务数据?
深度学习图像数据自动标注[通俗易懂]
【Leetcode】14. Longest Common Prefix
Global and Chinese market of oil analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Understand one article: four types of data index system
大廠面試總結大全