Problem :

https://leetcode.com/problems/diameter-of-binary-tree/description/


My Solution :

# 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):
"""
:type root: TreeNode
:rtype: int
"""
self.diameter = 1
self.find_depth(root)
return self.diameter - 1

def find_depth(self, root):
if not root:
return 0
left = self.find_depth(root.left)
right = self.find_depth(root.right)
self.diameter = max(self.diameter, 1 + left + right)
return 1 + max(left, right)


Comment :

이 문제는 좋은 방법이 떠오르지 않아 discuss를 참조하였다.