-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathsolution.py
60 lines (55 loc) · 1.52 KB
/
solution.py
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
#!/usr/bin/env python
# Created by Bruce yuan on 18-2-4.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None:
return 0
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
@staticmethod
def method_two(root):
if root is None:
return 0
import queue
q = queue.Queue()
q.put(root)
count = 0
# BFS 遍历
while not q.empty():
count += 1
for _ in range(q.qsize()):
item = q.get()
# 下面两个都要判断
if item.left is not None:
q.put(item.left)
if item.right is not None:
q.put(item.right)
return count
@staticmethod
def method_three(root):
# DFS
if root is None:
return 0
node = [root]
value = [1]
max_num = 0
while len(node) != 0:
item = node.pop()
tmp = value.pop()
max_num = max(tmp, max_num)
if item.left is not None:
node.push(item.left)
value.push(tmp + 1)
if item.right is not None:
node.push(item.right)
value.push(tmp + 1)
return max_num