-
Notifications
You must be signed in to change notification settings - Fork 0
/
543.diameter-of-binary-tree.py
44 lines (38 loc) · 1.41 KB
/
543.diameter-of-binary-tree.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
#
# @lc app=leetcode id=543 lang=python3
#
# [543] Diameter of Binary Tree
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
# add up the max depth of the left and max depth of the right tree
# save the number for every result returned
ans = self.diameterOfNode(root, 0)
return ans
def diameterOfNode(self, root: Optional[TreeNode], maxVal: int) -> int:
# add up the max depth of the left and max depth of the right tree
# save the number for every result returned
left = self.diameterOfNode(root.left, maxVal) if root.left else 0
right = self.diameterOfNode(root.right, maxVal) if root.right else 0
return max(self.maxDepth(root.left) + self.maxDepth(root.right), left, right)
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root:
left = self.maxDepth(root.left) if root.left else 0
right = (
self.maxDepth(
root.right,
)
if root.right
else 0
)
# need to +1 to indicate it is a different level
return max(left, right) + 1
return 0
# @lc code=end