FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
algorithm/Week_03/id_14/LeetCode_104_14.java at master · aiter/algorithm · GitHub
aiter
/
algorithm
Public
forked from
algorithm001/algorithm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
algorithm
/
Week_03
/
id_14
/
LeetCode_104_14.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
102 lines (83 loc) · 2.54 KB
Breadcrumbs
algorithm
/
Week_03
/
id_14
/
LeetCode_104_14.java
Copy path
File metadata and controls
102 lines (83 loc) · 2.54 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import
java
.
util
.
HashMap
;
import
java
.
util
.
Stack
;
/**
* https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
*
* <p> 简单
* <p> 二叉树
* <p> DFS
*
* @author aiter
* @date 2019/05/04 8:54 AM
*/
public
class
LeetCode_104_14
{
public
static
void
main
(
String
[]
args
) {
Solution
solution
=
new
Solution
();
Solution2
solution2
=
new
Solution2
();
/**
* <pre>
* 1
* / \
* 4 5
* / \ / \
* 4 5 6 7
* / \
* 12 5
* </pre>
*/
TreeNode
root
=
new
TreeNode
(
1
);
root
.
left
=
new
TreeNode
(
4
);
root
.
left
.
left
=
new
TreeNode
(
4
);
root
.
left
.
right
=
new
TreeNode
(
5
);
root
.
left
.
left
.
left
=
new
TreeNode
(
12
);
root
.
left
.
left
.
right
=
new
TreeNode
(
5
);
root
.
right
=
new
TreeNode
(
5
);
root
.
right
.
left
=
new
TreeNode
(
6
);
root
.
right
.
right
=
new
TreeNode
(
7
);
System
.
out
.
println
(
String
.
format
(
"期望值:4,实际值:%d"
,
solution
.
maxDepth
(
root
)));
System
.
out
.
println
(
String
.
format
(
"期望值:4,实际值:%d"
,
solution2
.
maxDepth
(
root
)));
}
/**
* 难得遇到一道送分题!
*/
static
class
Solution
{
public
int
maxDepth
(
TreeNode
root
) {
if
(
root
==
null
) {
return
0
;
}
int
left
=
maxDepth
(
root
.
left
);
int
right
=
maxDepth
(
root
.
right
);
return
((
left
>=
right
) ?
left
:
right
) +
1
;
}
}
/**
* 用stack和哈希表,实现一下。
* 哈希表记录当前节点的深度
*/
static
class
Solution2
{
public
int
maxDepth
(
TreeNode
root
) {
if
(
root
==
null
) {
return
0
;
}
int
max
=
0
;
Stack
<
TreeNode
>
stack
=
new
Stack
<>();
HashMap
<
TreeNode
,
Integer
>
map
=
new
HashMap
<>();
stack
.
push
(
root
);
map
.
put
(
root
,
map
.
getOrDefault
(
root
,
1
));
while
(!
stack
.
empty
()) {
TreeNode
cur
=
stack
.
pop
();
int
tmp
=
map
.
getOrDefault
(
cur
,
1
);
max
=
max
>
tmp
?
max
:
tmp
;
if
(
cur
.
right
!=
null
) {
stack
.
push
(
cur
.
right
);
map
.
put
(
cur
.
right
,
tmp
+
1
);
}
if
(
cur
.
left
!=
null
) {
stack
.
push
(
cur
.
left
);
map
.
put
(
cur
.
left
,
tmp
+
1
);
}
}
return
max
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL