-
Notifications
You must be signed in to change notification settings - Fork 2
/
MaximumDepthofBinaryTree.py
72 lines (63 loc) · 1.7 KB
/
MaximumDepthofBinaryTree.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
61
62
63
64
65
66
67
68
69
70
71
72
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
# DFS recursion (Bottom up solution)
class Solution1:
def maxDepth(self, root: TreeNode) -> int:
if not root:
return 0
left_depth = self.maxDepth(root.left)
right_depth = self.maxDepth(root.right)
return max(left_depth, right_depth) + 1
# DFS Iteration
class Solution2:
def maxDepth(self, root: TreeNode) -> int:
stack = []
if root:
stack.append((1, root))
depth = 0
while stack:
currentLevel,currentNode = stack.pop()
if currentNode is not None:
depth = max(depth, currentLevel)
stack.append((currentLevel+1, currentNode.left))
stack.append((currentLevel+1, currentNode.right))
return depth
# BFS Iteration
from collections import deque
class Solution3:
def maxDepth(self, root: TreeNode) -> int:
queue = deque()
if root:
queue.append((1, root))
depth = 0
while queue:
level, currentNode = queue.popleft()
if currentNode is not None:
queue.append((level+1, currentNode.left))
queue.append((level+1, currentNode.right))
depth = max(depth, level)
return depth
########
# Test #
########
# 3
# / \
# 9 20
# / \
# 15 7
root = TreeNode(3)
node1 = TreeNode(9)
node2 = TreeNode(20)
node3 = TreeNode(15)
node4 = TreeNode(7)
root.left = node1
root.right = node2
node2.left = node3
node2.right = node4
result = Solution3().maxDepth(root)
print(result)
# expected: 3