-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLC0271.py
24 lines (20 loc) · 935 Bytes
/
LC0271.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
class Solution:
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
# Define a recursive function to calculate the diameter
def diameter(node, res):
# Base case: if the node is None, return 0
if not node:
return 0
# Recursively calculate the diameter of left and right subtrees
left = diameter(node.left, res)
right = diameter(node.right, res)
# Update the maximum diameter encountered so far
res[0] = max(res[0], left + right)
# Return the depth of the current node
return max(left, right) + 1
# Initialize a list to hold the maximum diameter encountered
res = [0]
# Call the diameter function starting from the root
diameter(root, res)
# Return the maximum diameter encountered
return res[0]