-
Notifications
You must be signed in to change notification settings - Fork 233
/
node-depths.py
74 lines (61 loc) · 1.73 KB
/
node-depths.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
73
74
# NODE DEPTHS
# SOLUTION 1
def nodeDepths(root):
# Write your code here.
return calculateNodeDepths(root, 0)
def calculateNodeDepths(root, currentDepth):
if not root:
return 0
return currentDepth + calculateNodeDepths(root.left, currentDepth + 1) + calculateNodeDepths(root.right, currentDepth + 1)
# This is the class of the input binary tree.
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
# SOLUTION 2
def nodeDepths(root):
# Write your code here.
if not root:
return 0
totalDepth = 0
currentDepth = 0
levelOrderQueue = [root]
while levelOrderQueue:
totalDepth += len(levelOrderQueue) * currentDepth
currentDepth += 1
tempQueue = []
while levelOrderQueue:
tempNode = levelOrderQueue.pop()
if tempNode.left:
tempQueue.append(tempNode.left)
if tempNode.right:
tempQueue.append(tempNode.right)
levelOrderQueue = tempQueue
return totalDepth
# This is the class of the input binary tree.
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
#SOLUTION 3
def nodeDepths(root):
# Write your code here.
stack = [{"node": root, "depth": 0}]
totalDepth = 0
while len(stack) > 0:
print(stack)
tempNode = stack.pop()
if not tempNode["node"]:
continue
totalDepth += tempNode["depth"]
stack.append({"node": tempNode["node"].left, "depth": tempNode["depth"] + 1})
stack.append({"node": tempNode["node"].right, "depth": tempNode["depth"] + 1})
return totalDepth
# This is the class of the input binary tree.
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None