-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary_Tree_Diameter.py
More file actions
36 lines (31 loc) · 1.12 KB
/
Binary_Tree_Diameter.py
File metadata and controls
36 lines (31 loc) · 1.12 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
'''
Binary Tree Diameter:
Given a binary tree, output the diameter (length of its longest path,
regardless of if the path passes through the root)
Time: O(N)
Space: O(N), (Alternate O(D), where D is the depth of binary tree)
Last Practiced: 2022-03-23 07:19:27
'''
# This is an input class. Do not edit.
# This is an input class. Do not edit.
class BinaryTree:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
class TreeInfo:
def __init__(self, diameter, height):
self.diameter = diameter
self.height = height
def binaryTreeDiameter(tree):
return getTreeInfo(tree).diameter
def getTreeInfo(tree):
if tree is None:
return TreeInfo(0,0)
leftTreeInfo = getTreeInfo(tree.left)
rightTreeInfo = getTreeInfo(tree.right)
maxPathOverRoot = leftTreeInfo.height + rightTreeInfo.height
maxDiameterSoFar = max(leftTreeInfo.diameter, rightTreeInfo.diameter)
currentDiameter = max(maxPathOverRoot, maxDiameterSoFar)
currentHeight = 1 + max(leftTreeInfo.height, rightTreeInfo.height)
return TreeInfo(currentDiameter, currentHeight)