FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
leetcode/code/lc94.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
/
lc94.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
48 lines (46 loc) · 1.26 KB
Breadcrumbs
leetcode
/
code
/
lc94.java
Copy path
File metadata and controls
48 lines (46 loc) · 1.26 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
code
;
/*
* 94. Binary Tree Inorder Traversal
* 题意:
* 难度:Medium
* 分类:HashTable, Stack, Tree
* 思路:左节点依次入栈二叉树中序遍历
* Tips:和lc144前序,lc145后序一起看, lc102层次遍历
*/
import
java
.
util
.
ArrayList
;
import
java
.
util
.
List
;
import
java
.
util
.
Stack
;
public
class
lc94
{
public
class
TreeNode
{
int
val
;
TreeNode
left
;
TreeNode
right
;
TreeNode
(
int
x
) {
val
=
x
; }
}
public
List
<
Integer
>
inorderTraversal
(
TreeNode
root
) {
List
<
Integer
>
res
=
new
ArrayList
<>();
if
(
root
==
null
)
return
res
;
Stack
<
TreeNode
>
st
=
new
Stack
();
while
( !
st
.
isEmpty
() ||
root
!=
null
) {
//注意停止条件
while
(
root
!=
null
) {
st
.
push
(
root
);
root
=
root
.
left
;
}
root
=
st
.
pop
();
res
.
add
(
root
.
val
);
root
=
root
.
right
;
}
return
res
;
}
List
<
Integer
>
res
=
new
ArrayList
();
public
List
<
Integer
>
inorderTraversal2
(
TreeNode
root
) {
//递归
helper
(
root
);
return
res
;
}
public
void
helper
(
TreeNode
root
){
if
(
root
==
null
)
return
;
helper
(
root
.
left
);
res
.
add
(
root
.
val
);
helper
(
root
.
right
);
}
}
Back
|
FazBrowse Home
|
New Git URL