Given a tree, find the node with the minimum sum of distances to other nodes
2014-08-10 12:25
537 查看
O(n) complexity, have a traversal for the tree. Get the information of all children, then traverse the tree again.
#include <iostream> #include <vector> #include <algorithm> #include <utility> using namespace std; class Node { public: int cnum; vector<int> child; Node(int num):cnum(num) { } }; int buildTree(const vector<pair<int,int>>& edges, vector<int>& visited, vector<Node>& tree, int cur, int depth, int& sum) { int size = edges.size(), i; for (i = 0; i < size; ++i) if (visited[i] == 0 && (edges[i].first == cur || edges[i].second == cur)) { visited[i] = 1; int next = edges[i].first == cur ? edges[i].second : edges[i].first; sum += depth+1; tree[cur].child.push_back(next); tree[cur].cnum += buildTree(edges, visited, tree, next,depth+1, sum) + 1; } return tree[cur].cnum; } void dfs(vector<Node>& tree, int root, int& minsum, int& minroot, int cursum) { int i, size = tree[root].child.size(), next, n = tree.size(), m; for (i = 0; i < size; ++i) { next = tree[root].child[i], m = tree[next].cnum; int sum = cursum - m + (n - m -2); if (sum < minsum) { minsum = sum; minroot = next; } dfs(tree, next, minsum, minroot, sum); } } int getNode(const vector<pair<int, int>>& edges) { int size = edges.size(), root, sum = 0, minroot, minsum, cnum; if (size == 0) return 0; vector<Node> tree(size+1, Node(0)); vector<int> edgevisited(size, 0); root= edges[0].first, minroot = root; cnum = buildTree(edges, edgevisited, tree, root, 0, sum); minsum = sum; dfs(tree, root, minsum, minroot, sum); return minroot; } int main() { vector<pair<int, int>> edges; edges.push_back(make_pair(0,1)); edges.push_back(make_pair(0,2)); edges.push_back(make_pair(4,3)); edges.push_back(make_pair(1,3)); edges.push_back(make_pair(1,5)); edges.push_back(make_pair(6,5)); int res = getNode(edges); return 0; }
相关文章推荐
- Given an array of positive and negative integers find the first subarray with zero sum
- ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods
- Minimum no. of iterations to pass information to all nodes in the tree
- 【Leetcode】Given a binary tree, find all root-to-leaf paths whose sum equals a given number
- There is integer array like {1,2,4,5,6,1,2,4,3,5,7,2,1}. I want to find the possible combination of pair which sum is 4.
- perfect squares find the least number of perfect square numbers (1, 4, 9, 16, ...) which sum to n
- geeksforgeeks Find the maximum path sum between two leaves of a binary tree
- Q.2.3 Implement an algorithm to delete a node in the middle of a single linked list, given only acce
- Light is falling on a tree from left side you have to find all the nodes on which this light will fa
- No sources that matches the given name (at 'layout_toLeftOf' with value '@id/
- Unable to find a team with the given Team ID或者Failed to code sign的问题解决
- Ch4.8: find all the path that a Binary tree sums up to a given value, may/maynot have parent pointer
- In a bunch of number to find out the biggest minimum value
- Find the nearest common ancestor of any two nodes in a binary search tree
- How to find the correct version of JDeveloper to use with eBusiness Suite 11i or Release 12.x
- Unable to find a team with the given Team ID 问题解决方法
- leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree
- [CrackCode] 4.4 Creates a linked list of all the nodes at each depth of a given tree
- No resource found that matches the given name (at 'layout_toLeftOf' with value '@id/ history_child_
- 真机调试问题:Unable to find a team with the given Team ID