-
Notifications
You must be signed in to change notification settings - Fork 0
/
104_maximum-depth-of-binary-tree.py
51 lines (46 loc) · 1.33 KB
/
104_maximum-depth-of-binary-tree.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#@author: rye
#@time: 2019/3/30
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
'''
以前考研复习的时候也做过很多次,不过现在确实忘了。。。还得捡起来
'''
# 递归
class Solution(object):
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
else:
l_height = self.maxDepth(root.left)
r_height = self.maxDepth(root.right)
return max(l_height, r_height) + 1
# 非递归
class Solution1:
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
stack = []
if root is not None:
stack.append((1, root)) # 这里往stack里添加的是元组(depth, root)
depth = 0
while stack != []:
current_depth, root = stack.pop()
if root is not None:
depth = max(depth, current_depth)
stack.append((current_depth + 1, root.left))
stack.append((current_depth + 1, root.right))
return depth
if __name__ == '__main__':
pass