543. Diameter of Binary Tree
題目要算樹最遠兩個節點的間隔
我就爛遞迴算樹左右高然後加起來
再遞迴把整個樹的節點都算一次
超慢速才贏6.96%
class Solution:
def diameterOfBinaryTree(self, root: Optional[TreeNode]) -> int:
def Tree_height(root):
if root is not None:
return max(Tree_height(root.left), Tree_height(root.right)) +
1
else:
return 0
if root is not None:
path_length = Tree_height(root.left) + Tree_height(root.right)
return max(path_length, self.diameterOfBinaryTree(root.left),
self.diameterOfBinaryTree(root.right))
else:
return 0