Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
codinglin
leetcode
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
leetcode
/
src
/
JZOffer2
/
Main26.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
48 lines (46 loc) · 1.89 KB
master
Breadcrumbs
leetcode
/
src
/
JZOffer2
/
Main26.java
Copy path
Top
File metadata and controls
Code
Blame
48 lines (46 loc) · 1.89 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package JZOffer2;
/**
* 树的子结构
* 1.先序遍历树A中的每个节点n_A:对应函数 isSubStructure(A, B)
* 2.判断树A中以n_A为根节点的子树是否包含树B:对应函数 helper(root1, root2)
*
* helper(A, B) 函数:
* 终止条件:
* 当节点 B 为空:说明树 B 已匹配完成(越过叶子节点),因此返回 true;
* 当节点 A 为空:说明已经越过树 A 叶子节点,即匹配失败,返回 false;
* 当节点 A 和 B 的值不同:说明匹配失败,返回 false;
* 返回值:
* 判断 A 和 B 的左子节点是否相等,即 helper(A.left, B.left) ;
* 判断 A 和 B 的右子节点是否相等,即 helper(A.right, B.right) ;
* isSubStructure(A, B) 函数:
*
* 特例处理:当树 A 为空或树 B 为空时,直接返回 false ;
* 返回值:若树 B 是树 A 的子结构,则必满足以下三种情况之一,因此用或 || 连接;
* 以节点 A 为根节点的子树包含树 B,对应 helper(A, B);
* 树 B 是 树 A 左子树的子结构,对应 isSubStructure(A.left, B);
* 树 B 是 树 A 右子树的子结构,对应 isSubStructure(A.right, B);
*/
public class Main26 {
public boolean isSubStructure(TreeNode A, TreeNode B) {
if(A == null || B == null){
return false;
}
if(A.val == B.val && (helper(A.left, B.left) && helper(A.right, B.right))){
return true;
}
return isSubStructure(A.left, B) || isSubStructure(A.right, B);
}
private boolean helper(TreeNode root1, TreeNode root2) {
if(root2 == null){
return true;
}
if(root1 == null){
return false;
}
if(root1.val == root2.val){
return helper(root1.left, root2.left) && helper(root1.right, root2.right);
}else{
return false;
}
}
}
You can’t perform that action at this time.