Skip to content

Commit 5cee6c4

Browse files
committed
solve problem
1 parent 7795f81 commit 5cee6c4

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
func maxDepth(_ root: TreeNode?) -> Int {
3+
if root == nil {
4+
return 0
5+
}
6+
7+
var level = 0
8+
var stack: [(TreeNode?, Int)] = [(root, 1)]
9+
10+
while !stack.isEmpty {
11+
let (current, count) = stack.removeLast()
12+
if current?.left == nil && current?.right == nil {
13+
level = max(level, count)
14+
}
15+
if let right = current?.right {
16+
stack.append((right, count + 1))
17+
}
18+
if let left = current?.left {
19+
stack.append((left, count + 1))
20+
}
21+
}
22+
23+
return level
24+
}
25+
}
26+

0 commit comments

Comments
 (0)