#3820

Pythagorean Distance Nodes in a Tree

specialist · 760 · lc medium +31 · 57.7% accepted · 87 likes · top 53%

Description

An undirected tree with n nodes (labeled 0 to n - 1) is described by edges, where edges[i] = [ui, vi]. Three distinct target nodes x, y, and z are also provided. For each node u, let dx, dy, and dz be the edge counts on the paths from u to x, y, and z respectively. Node u is special if the sorted triple (dx, dy, dz) forms a Pythagorean triplet — three integers a <= b <= c satisfying a2 + b2 = c2. Return the count of special nodes in the tree.

Code

1
2
3