镜像站点
· 本页由第三方 GitHub 只读镜像提供,
非 GitHub 官方站点
,不接受任何登录或凭据输入。
前往 github.com
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
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 }}
LinXin04
LeetCode-Java
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
LeetCode-Java
/
FlattenBinaryTreeToLinkedList.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
64 lines (59 loc) · 1.39 KB
master
Breadcrumbs
LeetCode-Java
/
FlattenBinaryTreeToLinkedList.java
Copy path
Top
File metadata and controls
Code
Blame
64 lines (59 loc) · 1.39 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import javax.swing.tree.TreeNode;
/**
* 题目:将给定的二叉树转化为“只有右孩子节点”的链表。(前序遍历的方式)
* 例如:
* Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
* 解题思路:
* 首先判断左子树是否为空,不为空就寻找到树根的左孩子节点;
* 然后寻找该节点是否有右孩子,如果有继续寻找,直到找到属于叶子节点的右孩子;
* 此时,该节点的右子树“指向”当前树的右子树,并将当前左子树变为树根的右孩子,将整棵树左孩子置为空。
* 最后,根节点“指向”根节点的右孩子,继续上述操作,直到整棵树遍历完即得到结果。
*/
public class FlattenBinaryTreeToLinkedList {
}
class TreeNode25
{
int val;
TreeNode25 left;
TreeNode25 right;
TreeNode25(int x){val=x;}
}
class Solution131
{
public void flatten(TreeNode25 root)
{
if(root==null) return;
while(root!=null)
{
if(root.left!=null)
{
TreeNode25 cur=root.left;
//寻找最右边的元素
while(cur.right!=null)
cur=cur.right;
//将左子树插入到右子树中
cur.right=root.right;
root.right=root.left;
root.left=null;
}
root=root.right;
}
}
}
You can’t perform that action at this time.