class Solution: def longestUnivaluePath(self, root: TreeNode) -> int: self.ans=0 def arrow_length(node): if not node: return 0 left_length=arrow_length(node.left) right_length=arrow_length(node.right) left_arrow=right_arrow=0 if node.left and node.left.val==node.val: left_arrow=left_length+1 if node.right and node.right.val==node.val: right_arrow=right_length+1 self.ans=max(self.ans,left_arrow+right_arrow) return max(left_arrow,right_arrow) arrow_length(root) return self.ans
执行用时 :536 ms, 在所有 python3 提交中击败了40.17%的用户
内存消耗 :17.9 MB, 在所有 python3 提交中击败了6.10%的用户
——2019.11.21