[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.

Example 1:

Input: N = 6, edges = [[0,1],[0,2],[2,3],[2,4],[2,5]]
Output: [8,12,6,10,10,10]
Explanation:
Here is a diagram of the given tree:
  0
 /1   2
   /|  3 4 5
We can see that dist(0,1) + dist(0,2) + dist(0,3) + dist(0,4) + dist(0,5)
equals 1 + 1 + 2 + 2 + 2 = 8.  Hence, answer[0] = 8, and so on.

Github 同步地址:

https://github.com/grandyang/leetcode/issues/834

类似题目:
Distribute Coins in Binary Tree

参考资料:

https://leetcode.com/problems/sum-of-distances-in-tree/

LeetCode All in One 题目讲解汇总(持续更新中...)

原文地址:https://www.cnblogs.com/grandyang/p/11520804.html

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

[LeetCode] 834. 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

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)

LeetCode 404. Sum of Left Leaves (左子叶之和)

Find the sum of all left leaves in a given binary tree. Example: 3 / 9 20 / 15 7 There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 题目标签:Tree 这道题目给了我们一个二叉树,让我们找到所有左子叶之和.这里需要另外一个function - sumLeftLeaves 还要一个int

leetcode -day9 Candy &amp; Gas Station &amp; Binary Tree Maximum Path Sum

1.  Candy There are N children standing in a line. Each child is assigned a rating value. You are giving candies to these children subjected to the following requirements: Each child must have at least one candy. Children with a higher rating get m

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

[LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Pr

Leetcode:Path Sum 二叉树路径和

Path Sum: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return

LeetCode——Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 给定一个二叉树和一个值,找出所有根到叶的路径和等于