- Notifications
You must be signed in to change notification settings - Fork 306
/
Copy path104_maxDepth.py
36 lines (30 loc) · 829 Bytes
/
104_maxDepth.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Yu Zhou
# ****************
# Descrption:
# Given a binary tree, find its maximum depth.
# ****************
# 思路:
# 基础题,确保有Root就行。然后递归
# ****************
# Final Solution *
# ****************
classSolution(object):
defmaxDepth(self, root):
ifnotroot:
return0
else:
returnmax(self.maxDepth(root.left), self.maxDepth(root.right)) +1
# ********************
# Divide and Conquer *
# ********************
classSolution(object):
defmaxDepth(self, root):
ifnotroot:
return0
else:
# using Divide and Conquer
left=self.maxDepth(root.left)
right=self.maxDepth(root.right)
returnmax(left, right) +1