Skip to content
Navigation Menu
Toggle navigation
Sign in
Product
GitHub Copilot
Write better code with AI
GitHub Advanced Security
Find and fix vulnerabilities
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Discussions
Collaborate outside of code
Code Search
Find more, search less
Explore
Why GitHub
All features
Documentation
GitHub Skills
Blog
Solutions
By company size
Enterprises
Small and medium teams
Startups
Nonprofits
By use case
DevSecOps
DevOps
CI/CD
View all use cases
By industry
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
Topics
AI
DevOps
Security
Software Development
View all
Explore
Learning Pathways
Events & Webinars
Ebooks & Whitepapers
Customer Stories
Partners
Executive Insights
Open Source
GitHub Sponsors
Fund open source developers
The ReadME Project
GitHub community articles
Repositories
Topics
Trending
Collections
Enterprise
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 or jump to...
Search code, repositories, users, issues, pull requests...
Search syntax tips
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Sign in
Sign up
Reseting focus
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 }}
manasec
/
hacktoberfest2019-leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
40
Star
4
Code
Issues
0
Pull requests
11
Actions
Projects
0
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security
Insights
Files
master
Breadcrumbs
hacktoberfest2019-leetcode
/
Python3
/
Solution#617.py
Copy path
Blame
Blame
Latest commit
History
History
15 lines (13 loc) · 640 Bytes
master
Breadcrumbs
hacktoberfest2019-leetcode
/
Python3
/
Solution#617.py
Top
File metadata and controls
Code
Blame
15 lines (13 loc) · 640 Bytes
Raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
'''
This is the recursive solution I came up with
If both trees are empty then we return empty.
Otherwise, we will return a tree. The root value will be t1.val + t2.val, except these values are 0 if the tree is empty.
The left child will be the merge of t1.left and t2.left, except these trees are empty if the parent is empty.
The right child is similar.
'''
def
mergeTrees
(
self
,
t1
,
t2
):
if
not
t1
and
not
t2
:
return
None
ans
=
TreeNode
((
t1
.
val
if
t1
else
0
)
+
(
t2
.
val
if
t2
else
0
))
ans
.
left
=
self
.
mergeTrees
(
t1
and
t1
.
left
,
t2
and
t2
.
left
)
ans
.
right
=
self
.
mergeTrees
(
t1
and
t1
.
right
,
t2
and
t2
.
right
)
return
ans
You can’t perform that action at this time.