-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathNode-Depths.py
44 lines (33 loc) · 1.08 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
"""
The distance between a node in a Binary Tree and the tree's root alled the node's depth.
Write a function that takes in a Binary Tree and returns the sum of tis nodes' depths.
Each 'BinaryTree' node has an integer 'value', a 'left' child node, and a 'right' child node.
Children nodes can either be 'BinaryTree' nodes themselves or 'None/null'.
"""
class BinaryTree(object):
"""docstring for BinaryTree."""
def __init__(self, value):
self.value = value
self.left = None
self.right = None
import queue
def nodeDepths(root):
# Initialize an empty queue
Q = queue.Queue()
# Add root node and its depth to Q
Q.put((root, 0))
# Intialize result
result = 0
while(Q.qsize() > 0):
temp = Q.get()
# temp is a tuple with (node value, node depth)
node = temp[0]
depth = temp[1]
# Update result accordingly
result += depth
# BFS code
if (node.left):
Q.put((node.left, depth + 1))
if (node.right):
Q.put((node.right, depth + 1))
return result