Skip to content
This repository has been archived by the owner on Oct 14, 2021. It is now read-only.

Commit

Permalink
Added solution to sumofdistanceinTree (#541)
Browse files Browse the repository at this point in the history
* Added Soluion of ContainweWithMostWater problem

* Added Example

Signed-off-by: Pranjal Goyal <pranjalgoyal13@gmail.com>

* Added GenerateParentheses code

* Updates

Signed-off-by: Pranjal Goyal <pranjalgoyal13@gmail.com>

* Updated Solution of Sum Of Distance in Tree:

Signed-off-by: Pranjal Goyal <pranjalgoyal13@gmail.com>

* Updated Main method

Signed-off-by: Pranjal Goyal <pranjalgoyal13@gmail.com>
  • Loading branch information
pranjalg13 authored Oct 13, 2021
1 parent e616772 commit bee2398
Showing 1 changed file with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Question:
Suppose there is a tree with N nodes labelled from 0 to n-1
We have to return an array of length n where there is sum of each and every branch from root to leaf.
*/

/*
Steps to solve the algo:
--> Take node 0 as root
1. Initial an array of hashset tree, tree[i] contains all connected nodes to i.
2. Initial an array count, count[i] counts all nodes in the subtree i.
3. Initial an array of res, res[i] counts sum of distance in subtree i.
4.Post order dfs traversal, update count and res:
count[root] = sum(count[i]) + 1
res[root] = sum(res[i]) + sum(count[i])
5.
Pre order dfs traversal, update res:
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.
res[i] = res[root] - count[i] + N - count[i]
return res, done.
*/


import java.util.*;

class Solution {

int[] res, count;
ArrayList<HashSet<Integer>> tree;

public int[] sumOfDistancesInTree(int N, int[][] edges) {
tree = new ArrayList<HashSet<Integer>>();
res = new int[N];
count = new int[N];
for (int i = 0; i < N ; ++i)
tree.add(new HashSet<Integer>());
for (int[] e : edges) {
tree.get(e[0]).add(e[1]);
tree.get(e[1]).add(e[0]);
}
dfs(0, -1);
dfs2(0, -1);
return res;
}

public void dfs(int root, int pre) {
for (int i : tree.get(root)) {
if (i == pre) continue;
dfs(i, root);
count[root] += count[i];
res[root] += res[i] + count[i];
}
count[root]++;
}


public void dfs2(int root, int pre) {
for (int i : tree.get(root)) {
if (i == pre) continue;
res[i] = res[root] - count[i] + count.length - count[i];
dfs2(i, root);
}
}

public static void main(String [] args){
Solution s1 = new Solution();
int N = 4;
int[][] edges = new int[N][N];
s1.sumOfDistancesInTree(N, edges);

}
}

0 comments on commit bee2398

Please sign in to comment.