问题

给定一个二叉树的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. 深入浅出Spring(三)

    我为大家简单介绍了一下Spring框架核心内容中的IoC,接下来我们继续讲解另一个核心AOP(Aspect Oriented Programming),即面向切面编程. 1.OOP回顾 在介绍AOP之 ...

  2. 浅析vue混入(mixin)

    vue中的混入,可以在一定程度上提高代码的复用性.通俗来说,混入类似于“继承”,当前组件对象继承于组件对象,一般情况下遵循“就近原则”.但是与继承不同的是,继承一般都跟随着属性的重写与合并,混入在不同 ...

  3. fsockopen与HTTP 1.1/HTTP 1.0

    在前面的例子中,HTTP请求信息头有些指定了 HTTP 1.1,有些指定了 HTTP/1.0,有些又没有指定,那么他们之间有什么区别呢? 关于HTTP 1.1与HTTP 1.0的一些基本情况,可以参考 ...

  4. ASCII, Unicode, UTF-8

    (本文参考:http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html) 1. ASCII码 我们知道,在计算机内部,所有的 ...

  5. KETTLE多表关联的同步一张表的两种实现方式

    以下操作都在5.0.1版本下进行开发,其余版本可以进行自动比对 在平时工作当中,会遇到这种情况,而且很常见.比如:读取对方的多个视图或者表,写入目标库的一张表中,就涉及到多表的同步. 多表同步可以有以 ...

  6. MRC ARC 混编

    今天一个人问我 什么是MRC 什么是ARC 要是一个工程里用到了MRC和ARC 怎么办 我当时就无语了 什么情况 这是....   好了正经一点 我说一下iOS5.0以后就开始可以使用ARC( Aut ...

  7. Java修炼——面向对象的三大特征_封装的使用

    封装的作用含义:程序设计追求"高内聚,低耦合" 1.提高代码的安全性 2.提高代码的复用性 3."高内聚":封装细节,便于修改内部代码,提高可 维护性 4.&q ...

  8. JS的引入方式_变量的使用_变量的类型

    JS的俩种引入方式: 1. <!--js的引入方式1--> <script> /*网页中的弹框*/ alert("js的学习!!") </script ...

  9. Apache + WordPress 从 0 搭建

    引言 又到周末了,周末小编一般不更新系列文章,原因嘛是因为要攒稿子,年底工作比较忙,不攒点稿子是要断更的:( 所以周末一般聊点简单轻松加愉快的东西,小编现在的博客站是由 HEXO 进行搭建的,最近随着 ...

  10. [TimLinux] 系统配置 CentOS7配置Samba

    1. 安装软件 yum install -y samba samba-client samba-common 2. 配置用户 useradd tim passwd tim # 设置用户登录密码 smb ...