Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions C++/Sum-of-distances-in-tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class Solution
{
public:
/*
Uses classics Tree Re-rooting DP,
Need to do 2 traversals: DFSes but 1 as Post Order and second as Pre-order

When we move our root from parent to its child i,
count[i] points get 1 closer to root, n - count[i] nodes get 1 futhur to root.

Time Complexity: O(N) because both DFS visit all ndoes
Space Compexity: O(N) because of the resultant & helper subtree-node-count array

*/

/* Post Order to update distances while going from parent to root
To track a count of number of children in the subtree
*/
void dfs1(int node, int parent, vector<int> &res, vector<int> &count, vector<vector<int>> &graph)
{
for (auto child : graph[node])
{
if (child == parent)
continue;
dfs1(child, node, res, count, graph);
count[node] += count[child];
res[node] += res[child] + count[child];
}
}

/* Pre order traversal style DFS, to update the result vector in direction of child -> parent */
void dfs2(int node, int parent, vector<int> &res, vector<int> &count, vector<vector<int>> &graph, int n)
{
for (auto child : graph[node])
{
if (child == parent)
continue;
res[child] = res[node] - count[child] + n - count[child];
dfs2(child, node, res, count, graph, n);
}
}

vector<int> sumOfDistancesInTree(int n, vector<vector<int>> &edges)
{

vector<int> count(n, 1); //stores count of number of children, atleast 1 node, ie itself should be in count[node]
vector<int> res(n, 0); //Stores final distance sum for each node

vector<vector<int>> graph(n);

int u, v;

// Constructing the graph from the given array
for (int i = 0; i < edges.size(); ++i)
{
u = edges[i][0], v = edges[i][1];
graph[u].push_back(v);
graph[v].push_back(u);
}

dfs1(0, -1, res, count, graph);
dfs2(0, -1, res, count, graph, n);

return res;
}
};
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [C++](./C++/Spiral-matrix.cpp) | O(M\*N) | O(M\*N) | Medium | Array |
| 238 | [Product of Array Except Self](https://leetcode.com/problems/product-of-array-except-self/) | [C++](./C++/238.Product_of_array_except_self) | O(N) | O(N) | Medium | Array |


<br/>

<div align="right">
Expand Down Expand Up @@ -396,7 +397,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 070 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Java](./Java/climbing-stairs.java) | _O(N)_ | _O(1)_ | Easy | DP | |
| 730 | [Count Different Palindromic Subsequences](https://leetcode.com/problems/count-different-palindromic-subsequences/) | [C++](./C++/Count-Different-Palindromic-Subsequences.cpp) | _O(N\*N)_ | _O(N\*N)_ | Hard | DP | |
| 55 | [Jump Game](https://leetcode.com/problems/jump-game/) | [Python](./Python/jumpGame.py) | _O(N)_ | _O(N)_ | Medium | DP | |
| 337 | [House Robber 3](https://leetcode.com/problems/house-robber-iii/) | [C++](./C++/House-Robber-3.cpp) | _O(n)_ | _O(n)_ | Medium | DP Tree DFS Binary Tree |
| 337 | [House Robber 3](https://leetcode.com/problems/house-robber-iii/) | [C++](./C++/House-Robber-3.cpp) | _O(n)_ | _O(n)_ | Medium | DP Tree DFS Binary Tree | |

<br/>
<div align="right">
Expand Down