Skip to content

Commit e704ab3

Browse files
committed
104 Maximum Depth of Binary Tree.py
1 parent 34791b8 commit e704ab3

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

104 Maximum Depth of Binary Tree.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Definition for a binary tree node.
2+
class TreeNode(object):
3+
def __init__(self, val=0, left=None, right=None):
4+
self.val = val
5+
self.left = left
6+
self.right = right
7+
8+
class Solution(object):
9+
def maxDepth(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
if root:
15+
left_depth = 0
16+
right_depth = 0
17+
if not root.left and not root.right:
18+
return 1
19+
if root.left:
20+
left_depth = 1 + self.maxDepth(root.left)
21+
if root.right:
22+
right_depth = 1 + self.maxDepth(root.right)
23+
24+
return max(left_depth,right_depth)
25+
return 0
26+

0 commit comments

Comments
 (0)