문제
동일한 값을 지닌 가장 긴 경로를 찾아라.
입력
[5,4,5,1,1,5]
출력
2
풀이
class Solution(object): result = 0 def longestUnivaluePath(self, root): """ :type root: TreeNode :rtype: int """ def dfs(node): if node is None: return 0 left = dfs(node.left) right = dfs(node.right) if node.left and node.val == node.left.val: left += 1 else: left = 0 if node.right and node.val == node.right.val: right += 1 else: right = 0 self.result = max(self.result, left+right) return max(left, right) dfs(root) return self.result dfs(root) return self.longest