Problem solution · C++

Pythagorean Distance Nodes in a Tree

Pythagorean Distance Nodes in a Tree: a C++ solution using breadth-first search. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Breadth-first search
Source
Kamyu LeetCode Solutions
Length
47 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Breadth-first search

For Pythagorean Distance Nodes in a Tree, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.

  1. Model each valid configuration as a state and each legal move as an edge.
  2. Seed the queue with the starting state and mark it immediately.
  3. Expand each state once, recording distance or reachability for unseen neighbours.

Code notes

  • 47 lines of C++ from the credited upstream file pythagorean-distance-nodes-in-a-tree.cpp.
  • The implementation visibly relies on sequence storage.
  • 5 loop blocks detected.

Complexity

Verify that each state and transition is processed only a bounded number of times; that determines the traversal cost.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codePythagorean Distance Nodes in a Tree · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(n)// Space: O(n) // bfsclass Solution {public:    int specialNodes(int n, vector<vector<int>>& edges, int x, int y, int z) {        vector<vector<int>> adj(n);        const auto& bfs = [&](int u) {            vector<int> dist(n, -1);            dist[u] = 0;            vector<int> q = {u};            while (!empty(q)) {                vector<int> new_q;                for (const auto& u : q) {                    for (const auto& v : adj[u]) {                        if (dist[v] != -1) {                            continue;                        }                        dist[v] = dist[u] + 1;                        new_q.emplace_back(v);                    }                }                q = move(new_q);            }            return dist;        };         for (const auto& e : edges) {            adj[e[0]].emplace_back(e[1]);            adj[e[1]].emplace_back(e[0]);        }        const auto& dist1 = bfs(x);        const auto& dist2 = bfs(y);        const auto& dist3 = bfs(z);        int result = 0;        for (int u = 0; u < n; ++u) {            const int64_t a = dist1[u], b = dist2[u], c = dist3[u];            const auto& mx = max({a, b, c});            if (a * a + b * b + c * c == 2 * mx * mx) {                ++result;            }        }        return result;    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗