Given the root of a binary tree, return the length of the longest path, where each node in the path has the same value. This path may or may not pass through the root.The length of the path between two nodes is represented by the number of edges between them.
from leetcode_py import TreeNodeclass Solution: # Time: O(n) # Space: O(h) def longest_univalue_path(self, root: TreeNode[int] | None) -> int: # Iterative post-order: depth can reach 1000, so avoid recursion limits. best = 0 stack: list[tuple[TreeNode[int] | None, bool]] = [(root, False)] if root else [] arrow: dict[int, int] = {} while stack: node, processed = stack.pop() if node is None: continue if not processed: stack.append((node, True)) stack.append((node.left, False)) stack.append((node.right, False)) continue left = 0 left_child = node.left if left_child is not None and left_child.val == node.val: left = arrow[id(left_child)] + 1 right = 0 right_child = node.right if right_child is not None and right_child.val == node.val: right = arrow[id(right_child)] + 1 arrow[id(node)] = max(left, right) best = max(best, left + right) return best