X Tutup
root = {'data': 'A', 'children': [{'data': 'B', 'children': [{'data': 'D', 'children': []}]}, {'data': 'C', 'children': [{'data': 'E', 'children': [{'data': 'G', 'children': []}, {'data': 'H', 'children': []}]}, {'data': 'F', 'children': []}]}]} def getDepth(node): if len(node['children']) == 0: # BASE CASE return 0 else: # RECURSIVE CASE maxChildDepth = 0 for child in node['children']: # Find the depth of each child node: childDepth = getDepth(child) if childDepth > maxChildDepth: # This child is deepest child node found so far: maxChildDepth = childDepth return maxChildDepth + 1 print('Depth of tree is ' + str(getDepth(root)))
X Tutup