Approach
Breadth-first search
For Maximum Distinct Path Sum in a Binary Tree, the implementation explores reachable states in layers, which is the standard shape for unweighted shortest paths and minimum-step transitions.
- Model each valid configuration as a state and each legal move as an edge.
- Seed the queue with the starting state and mark it immediately.
- Expand each state once, recording distance or reachability for unseen neighbours.
Code notes
- 121 lines of C++ from the credited upstream file maximum-distinct-path-sum-in-a-binary-tree.cpp.
- The implementation visibly relies on sequence storage, hash lookup.
- 9 loop blocks detected, together with recursive traversal.
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.
Use this to learn the idea, then write your own version.
123 45class Solution {6public:7 int maxSum(TreeNode* root) {8 const auto& bfs = [&]() {9 vector<vector<int>> adj(1);10 vector<int> vals = {root->val};11 vector<pair<TreeNode*, int>> q = {{root, 0}};12 while (!empty(q)) {13 vector<pair<TreeNode*, int>> new_q;14 for (const auto& [u, p] : q) {15 vals.emplace_back(u->val);16 adj.emplace_back();17 const auto& i = size(adj) - 1;18 if (p != -1) {19 adj[i].emplace_back(p);20 adj[p].emplace_back(i);21 }22 for (const auto& node : {u->left, u->right}) {23 if (!node) {24 continue;25 }26 new_q.emplace_back(node, i);27 }28 }29 q = move(new_q);30 }31 return pair(adj, vals);32 };33 34 const auto& [adj, vals] = bfs();35 const auto iter_dfs = [&](int u) {36 int result = numeric_limits<int>::min();37 int total = 0;38 unordered_set<int> lookup;39 vector<tuple<int, int, int>> stk = {{1, u, -1}};40 while (!empty(stk)) {41 const auto [step, u, p] = stk.back(); stk.pop_back();42 if (step == 1) {43 if (lookup.count(vals[u])) {44 continue;45 }46 stk.emplace_back(2, u, p);47 lookup.emplace(vals[u]);48 total += vals[u];49 result = max(result, total);50 for (const auto& v : adj[u]) {51 if (v == p) {52 continue;53 }54 stk.emplace_back(1, v, u);55 }56 } else if (step == 2) {57 total -= vals[u];58 lookup.erase(vals[u]);59 }60 }61 return result;62 63 };64 int result = numeric_limits<int>::min();65 for (int u = 0; u < size(adj); ++u) {66 result = max(result, iter_dfs(u));67 }68 return result;69 }70};71 72737475class Solution2 {76public:77 int maxSum(TreeNode* root) {78 vector<vector<int>> adj;79 vector<int> vals;80 const auto dfs1 = [&](this auto&& dfs1, TreeNode *u, int p) -> void {81 vals.emplace_back(u->val);82 adj.emplace_back();83 const auto& i = size(adj) - 1;84 if (p != -1) {85 adj[i].emplace_back(p);86 adj[p].emplace_back(i);87 }88 for (const auto& node : {u->left, u->right}) {89 if (!node) {90 continue;91 }92 dfs1(node, i);93 }94 };95 96 unordered_set<int> lookup;97 const auto dfs2 = [&](this auto&& dfs2, int u, int p) {98 if (lookup.count(vals[u])) {99 return numeric_limits<int>::min();100 }101 lookup.emplace(vals[u]);102 int mx = 0;103 for (const auto& v : adj[u]) {104 if (v == p) {105 continue;106 }107 mx = max(mx, dfs2(v, u));108 }109 lookup.erase(vals[u]);110 return vals[u] + mx;111 };112 113 dfs1(root, -1);114 int result = numeric_limits<int>::min();115 for (int u = 0; u < size(adj); ++u) {116 result = max(result, dfs2(u, -1));117 }118 return result;119 }120};121