leetcode834 Sum of Distances in Tree

思路:

树形dp。

实现:

 1 class Solution
 2 {
 3 public:
 4     void dfs(int root, int p, vector<vector<int>>& G, vector<int>& cnt, vector<int>& res)
 5     {
 6         for (auto it: G[root])
 7         {
 8             if (it == p) continue;
 9             dfs(it, root, G, cnt, res);
10             cnt[root] += cnt[it];
11             res[root] += res[it] + cnt[it];
12         }
13         cnt[root]++;
14     }
15     void dfs2(int root, int p, vector<vector<int>>& G, vector<int>& cnt, vector<int>& res, int N)
16     {
17         for (auto it: G[root])
18         {
19             if (it == p) continue;
20             res[it] = res[root] - cnt[it] + N - cnt[it];
21             dfs2(it, root, G, cnt, res, N);
22         }
23     }
24     vector<int> sumOfDistancesInTree(int N, vector<vector<int>>& edges)
25     {
26         vector<vector<int>> G(N, vector<int>());
27         for (auto it: edges)
28         {
29             int a = it[0], b = it[1];
30             G[a].push_back(b); G[b].push_back(a);
31         }
32         vector<int> cnt(N, 0), res(N, 0);
33         dfs(0, -1, G, cnt, res);
34         dfs2(0, -1, G, cnt, res, N);
35         return res;
36     }
37 }

原文地址:https://www.cnblogs.com/wangyiming/p/11622629.html

时间: 2024-11-07 18:43:30

leetcode834 Sum of Distances in Tree的相关文章

834. Sum of Distances in Tree —— weekly contest 84

Sum of Distances in Tree An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0] and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i

[Swift]LeetCode834. 树中距离之和 | Sum of Distances in Tree

An undirected, connected tree with N nodes labelled 0...N-1 and N-1 edges are given. The ith edge connects nodes edges[i][0]and edges[i][1] together. Return a list ans, where ans[i] is the sum of the distances between node i and all other nodes. Exam

[LeetCode] 834. Sum of Distances in Tree 树中距离之和

An undirected, connected?tree with?N?nodes labelled?0...N-1?and?N-1?edges?are?given. The?ith edge connects nodes?edges[i][0]?and?edges[i][1]?together. Return a list?ans, where?ans[i]?is the sum of the distances between node?i?and all other nodes. Exa

Given a tree, find the node with the minimum sum of distances to other nodes

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 { publ

2 Sum in Binary Search Tree

Given a BST, find 2 nodes in it which sum to a given target hint:  Inorder traversal + BST + 2 SUM * Time : O(N) * Space: O(lgN) 1 class TreeNode0 { 2 int val; 3 TreeNode0 left, right; 4 public TreeNode0(int val) { 5 this.val = val; 6 } 7 } 8 public

【LeetCode】深搜DFS(共85题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [104]Maximum Depth of Binary Tree [105]Construct Binary Tree from Preorder and Inorder

【LeetCode】树(共94题)

[94]Binary Tree Inorder Traversal [95]Unique Binary Search Trees II (2018年11月14日,算法群) 给了一个 n,返回结点是 1 - n 的所有形态的BST. 题解:枚举每个根节点 r, 然后递归的生成左右子树的所有集合,然后做笛卡尔积. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *

LeetCode: Binary Tree Maximum Path Sum 解题报告

Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example:Given the below binary tree, 1      / \     2   3 SOLUTION 1: 计算树的最长path有2种情况: 1. 通过根的path. (1)如果左子树从左树根到任何一个N

Binary Tree Maximum Path Sum 自底向上求解(重重重)

题目: 链接 解答: 自底向上求解.left_max right_max分别返回了左右子树的最大路径和,如果左右子树最大路径和小于0,那么返回零, 用这个最大路径和和根节点的值相加,来更新最大值,同时, 更新返回该树的最大路径值. 代码: class Solution { public: int max = INT_MIN; int maxPathSum(TreeNode *root) { if (root == NULL) return 0; search(root); return max;