FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc116.java at master · mJackie/leetcode · GitHub
mJackie
/
leetcode
Public
Notifications
You must be signed in to change notification settings
Fork
135
Star
405
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
leetcode
/
code
/
lc116.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
44 lines (42 loc) · 1.48 KB
Breadcrumbs
leetcode
/
code
/
lc116.java
Copy path
File metadata and controls
44 lines (42 loc) · 1.48 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
package
code
;
/*
* 116. Populating Next Right Pointers in Each Node
* 题意:设置二叉树的next指针,指向同层右侧节点
* 难度:Medium
* 分类:Tree, Depth-first Search
* 思路:自己写的递归,中间有冗余。迭代方法和较优的递归会利用先前已经设置的next指针,做下一步操作
* Tips:复习时多看一下,自己没想起来利用已经设置好的指针
*/
public
class
lc116
{
public
class
TreeLinkNode
{
int
val
;
TreeLinkNode
left
,
right
,
next
;
TreeLinkNode
(
int
x
) {
val
=
x
; }
}
public
void
connect
(
TreeLinkNode
root
) {
if
(
root
==
null
)
return
;
helper
(
root
.
left
,
root
.
right
);
}
public
void
helper
(
TreeLinkNode
root1
,
TreeLinkNode
root2
){
//较慢,也能过
if
(
root1
==
null
||
root2
==
null
)
return
;
root1
.
next
=
root2
;
helper
(
root1
.
left
,
root1
.
right
);
helper
(
root1
.
right
,
root2
.
left
);
//会重复设置
helper
(
root2
.
left
,
root2
.
right
);
}
public
void
connect2
(
TreeLinkNode
root
) {
while
(
root
!=
null
){
TreeLinkNode
start
=
root
;
while
(
start
!=
null
){
if
(
start
.
left
!=
null
){
start
.
left
.
next
=
start
.
right
;
//设置下一层的next指针
if
(
start
.
next
!=
null
){
start
.
right
.
next
=
start
.
next
.
left
;
}
}
start
=
start
.
next
;
}
root
=
root
.
left
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL