-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path104_Maximum_Depth_of_BinaryTree.py
More file actions
64 lines (55 loc) · 1.39 KB
/
104_Maximum_Depth_of_BinaryTree.py
File metadata and controls
64 lines (55 loc) · 1.39 KB
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
61
62
63
64
# -*- coding:utf-8 -*-
# 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
"""
stack=[]
res=0
if root:
stack.append(root)
while(stack):
for i in range(len(stack)):
node=stack.pop(0)
if node.left:
stack.append(node.left)
if node.right:
stack.append(node.right)
res += 1
return res
#递归解法
def maxDepth1(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
left_depth=self.maxDepth1(root.left)
right_depth=self.maxDepth1(root.right)
max=left_depth if left_depth>right_depth else right_depth
max += 1
return max
if __name__ == "__main__":
node1=TreeNode(3)
node2=TreeNode(9)
node3=TreeNode(20)
node4 = TreeNode(15)
node5 = TreeNode(7)
node6 = TreeNode(2)
node7 = TreeNode(3)
node1.left=node2
node1.right = node3
# node2.left = node4
# node2.right = node5
node3.left = node4
node3.right = node5
r=Solution().maxDepth1(node1)
print r