FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SwordForOfferJava/problem27/Convert.java at master · lsp12138/SwordForOfferJava · GitHub
lsp12138
/
SwordForOfferJava
Public
Notifications
You must be signed in to change notification settings
Fork
5
Star
9
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SwordForOfferJava
/
problem27
/
Convert.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
61 lines (61 loc) · 1.7 KB
Breadcrumbs
SwordForOfferJava
/
problem27
/
Convert.java
Copy path
File metadata and controls
61 lines (61 loc) · 1.7 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
package
problem27
;
/*
* 面试题27:二叉搜索树与双向链表
* 将二叉搜索树转换成一个排序的双向链表,要求不能创建新的结点,只能改变树中结点的指向。
* 思路:搜索树的中序遍历结果是排好序的,如果把它变成排序的双向链表,根结点一定和左子树里最大的结点相连,也一定和右子树最小的结点相连。然后对于左右子树的根结点,递归处理。
*/
public
class
Convert
{
private
BiTree
convert
(
BiTree
root
){
//这里返回二叉树表示的双向链表
BiTree
lastNode
=
null
;
//指向双向链表尾端(值最大的结点)
BiTree
headNode
=
convertNode
(
root
,
lastNode
);
//后边函数返回的是双向链表尾结点,我们需要头结点
while
(
headNode
!=
null
&&
headNode
.
left
!=
null
){
headNode
=
headNode
.
left
;
}
return
headNode
;
}
private
BiTree
convertNode
(
BiTree
root
,
BiTree
lastNode
){
if
(
root
==
null
){
return
null
;
}
BiTree
current
=
root
;
//按照中序遍历左根右的顺序来,左
if
(
current
.
left
!=
null
){
lastNode
=
convertNode
(
current
.
left
,
lastNode
);
}
//根,此时lastNode应是左子树的最大值,与根相连后,根变成了新的lastNode
current
.
left
=
lastNode
;
if
(
lastNode
!=
null
){
lastNode
.
right
=
current
;
}
lastNode
=
current
;
//右
if
(
current
.
right
!=
null
){
lastNode
=
convertNode
(
current
.
right
,
lastNode
);
}
return
lastNode
;
}
public
static
void
main
(
String
[]
args
) {
Convert
test
=
new
Convert
();
BiTree
A1
=
new
BiTree
(
4
);
BiTree
A2
=
new
BiTree
(
2
);
BiTree
A3
=
new
BiTree
(
5
);
BiTree
A4
=
new
BiTree
(
1
);
BiTree
A5
=
new
BiTree
(
3
);
A1
.
left
=
A2
;
A1
.
right
=
A3
;
A2
.
left
=
A4
;
A2
.
right
=
A5
;
BiTree
a
=
test
.
convert
(
A1
);
while
(
a
!=
null
){
System
.
out
.
println
(
a
.
value
);
a
=
a
.
right
;
}
}
}
class
BiTree
{
int
value
;
BiTree
left
,
right
;
BiTree
(
int
x
){
value
=
x
;
}
}
Back
|
FazBrowse Home
|
New Git URL