class Solution: def diameterOfBinaryTree(self, root): """ :type root: TreeNode :rtype: int """ self.length = 0 def depth(node): if not node: return 0 left = depth(node.left) right = depth(node.right) self.length = max(self.length, left + right) return 1 + max(left, right) depth(root) return self.length
|