题目
代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def diameterOfBinaryTree(self, root: TreeNode) -> int:
self.ans = 0
def depth(root):
if not root: return 0
left = depth(root.left)
right = depth(root.right)
# path
self.ans = max(self.ans, left + right)
# depth
return max(left, right) + 1
depth(root)
return self.ans