问题

给定一个二叉树的root节点,二叉树中每个节点有node.val个coins,一种有N coins。 现在要求移动节点中的coins 使得二叉树最终每个节点的coins value都为1。每次移动,我们只能向相邻接点移动,也就是说coins 只能向父节点或者子节点移动,那么求最终需要移动多少步。

Leetcode原题链接: https://leetcode.com/problems/distribute-coins-in-binary-tree/

分析

如果叶子节点有0个coins,那么我们需要从parent节点移一个coin到叶子节点 (push (-1) 个coin from left node); ----move一次

如果说叶子节点有四个节点那么我们则需要push三个coins到父节点(push +3 coins from leaf node)。----move 三次

定义一个dfs(node)函数,用于表示需要从当前节点push 到its parent节点的coins个数(换句话说就是以当前节点为root的子树中的硬币总数 减去当前总数节点的个数),那么dfs(node) = dfs(node.left) + dfs(node.right) - 1 (减去一个1是因为留一个coin在当前节点) ,而使得以当前节点为root节点需要移动的步数等于abs(dfs(node.left)) + abs(dfs(node.right))。

实现

  private int ans = 0;
public int distributeCoins(TreeNode root) {
ans = 0;
distributeCoinsHelper(root);
return ans;
}
private int dfs(TreeNode cur){
if(cur == null){
return 0;
}
int left = dfs(cur.left);
int right = dfs(cur.right);
ans += Math.abs(left) + Math.abs(right);
return left + right + cur.val - 1;
}

Leetcode979 : Distribute Coins in Binary Tree 二叉树均匀分配硬币问题的更多相关文章

  1. LC 979. Distribute Coins in Binary Tree

    Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there ar ...

  2. [Swift]LeetCode979. 在二叉树中分配硬币 | Distribute Coins in Binary Tree

    Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there ar ...

  3. 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  4. LeetCode 979. Distribute Coins in Binary Tree

    原题链接在这里:https://leetcode.com/problems/distribute-coins-in-binary-tree/ 题目: Given the root of a binar ...

  5. 【leetcode】979. Distribute Coins in Binary Tree

    题目如下: Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and th ...

  6. 二叉树分派硬币 Distribute Coins in Binary Tree

    2019-03-27 15:53:38 问题描述: 问题求解: 很有意思的题目.充分体现了二叉树的自底向上的递归思路. 自底向上进行运算,对于最底层的二叉子树,我们需要计算每个节点向其parent传送 ...

  7. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  8. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  9. [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

随机推荐

  1. 08-kubernetes 存储卷

    目录 存储卷 emptyDir 测试及使用 Pod测试挂在共享NFS 写测试清单 测试 pv, pvc 创建几个PV 创建测试的Pod 和 PVC 存储卷 分为四种: 有状态,需要存储 有状态,无需存 ...

  2. PHP自动发红包代码示例

    <?php header('Content-type:text'); define("TOKEN", "weixin"); $wechatObj = ne ...

  3. 图的存储结构与操作--C语言实现

    图(graph)是一种比树结构还要复杂的数据结构,它的术语,存储方式,遍历方式,用途都比较广,所以如果想要一次性完成所有的代码,那代码会非常长.所以,我将分两次来完成图的代码.这一次,我会完成图的五种 ...

  4. 深入 .NET Core 基础 - 2:共享框架

    深入 .NET Core 基础 - 2:共享框架 原文地址:https://natemcmaster.com/blog/2018/08/29/netcore-primitives-2/ 共享框架从 . ...

  5. 大型情感剧集Selenium:4_老中医教你(单/多/下拉框)选项定位 #华为云·寻找黑马程序员#

    今天讲什么 讲什么标题说了,讲selenium的单选.多选.下拉框选项定位.但其实这东西,没什么太多说的,又比较枯燥,那该怎么让这一集selenium的课程变得有趣呢?有请老中医,哈哈- 怎么样,这个 ...

  6. ModelArts微认证零售客户分群知识点总结

    \ 作者:华为云MVP郑永祥

  7. 手撕 JVM 垃圾收集日志

    下图是本篇的写作大纲,将从以下四个方面介绍怎么样处理 JVM 日志. 有准备才能不慌 想要分析日志,首先你得有日志呀,对不对.凡是未雨绸蒙总是没错的.所谓有日志的意思,你要把 JVM 参数配置好,日志 ...

  8. P4072 [SDOI2016](BZOJ4518) 征途 [斜率优化DP]

    题目描述 Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜.所以,一段路 ...

  9. SpringBoot学习【一】----- HelloWord

    springboot是什么 Spring Boot可以轻松创建可以运行的独立的,生产级的基于Spring的应用程序. 大多数Spring Boot应用程序只需要很少的Spring配置. 提供了一个运行 ...

  10. 【JS】370- 总结异步编程的六种方式

    点击上方"前端自习课"关注,学习起来~ 作者:Aima https://segmentfault.com/a/1190000019188824 众所周知 JavaScript 是  ...