题目如下:

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

In one move, we may choose two adjacent nodes and move one coin from one node to another.  (The move may be from parent to child, or from child to parent.)

Return the number of moves required to make every node have exactly one coin.

Example 1:

Input: [3,0,0]
Output: 2
Explanation: From the root of the tree, we move one coin to its left child, and one coin to its right child.

Example 2:

Input: [0,3,0]
Output: 3
Explanation: From the left child of the root, we move two coins to the root [taking two moves]. Then, we move one coin from the root of the tree to the right child.

Example 3:

Input: [1,0,2]
Output: 2

Example 4:

Input: [1,0,0,null,3]
Output: 4

Note:

  1. 1<= N <= 100
  2. 0 <= node.val <= N

解题思路:这个题目有点意思,我的方法是“借”的思想。对于任意一个叶子节点,如果val是0,那么表示要向其父节点取一个coin,那么parent.val -= 1, moves += 1;如果是叶子节点的val大于1,那么表示要给父节点val-1个coin,同时moves += (val-1)。当然这两种情况可以用通用的表达式:move += abs(node.val - 1), parent.val += (node.val - 1)。按照后序遍历的方式即可算出总的move次数。

代码如下:

# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution(object):
res = 0
def recursive(self,node,parent):
if node.left != None:
self.recursive(node.left,node)
if node.right != None:
self.recursive(node.right,node)
self.res += abs(node.val - 1)
if parent != None:
parent.val += (node.val - 1)
def distributeCoins(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.res = 0
self.recursive(root,None)
return self.res

【leetcode】979. Distribute Coins in Binary Tree的更多相关文章

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

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

  2. 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)

    [LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...

  3. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

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

  5. LeetCode 979. Distribute Coins in Binary Tree

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

  6. 【LeetCode】111. Minimum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 [LeetCode] 题目地址 ...

  7. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

  8. 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...

  9. 【LeetCode】104 - Maximum Depth of Binary Tree

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

随机推荐

  1. Debian取消从光盘安装软件的方式(please insert the disc labeled)

    与Ubuntu不同,使用apt-get install packages时Debian可能会提示: Media change: please insert the disc labeled 'Debi ...

  2. mysql Got a packet bigger than 'max_allowed_packet' bytes

    背景 数据库备份执行SQL文件时,执行到图片表插入图片数据时错误: 错误提示:Got a packet bigger than 'max_allowed_packet' bytes 原因分析及解决 m ...

  3. 企业打开云HBase的正确方式,来自阿里云云数据库团队的解读

    一.HBase的历史由来 HBase是一个开源的非关系型分布式数据库(NoSQL),基于谷歌的BigTable建模,是一个高可靠性.高性能.高伸缩的分布式存储系统,使用HBase技术可在廉价PC Se ...

  4. vue父组件异步数据子组件接收遇到的坑

    大家都知道父组件给子组件传值,子组件给父组件传值,两者通信并不难,官网上也有给案例,但是如果子组件想拿到父组件的异步数据,常规的写法是不行的,下面我记录我常用的两者写法: 方法1: 子组件用v-if, ...

  5. 【CF1210B】Marcin and Training Camp(贪心)

    题意:有n个人,60种技能点,如果第i个人会第j种技能a[i]的二进制表示的第j位就是1,第i个人的价值是b[i] 如果有若干种技能i会j不会,i就会鄙视j 求一种至少两个人的选人方案使得价值和最大, ...

  6. [CSP-S模拟测试73]题解

    A.小P的2048 作为一个看B哥玩了一个寒假的人这种题闭眼切好吧 模拟即可.程序模块化后直接复制粘贴. 说什么模拟不能复制粘贴的都没水平 #include<cstdio> #includ ...

  7. 6105 - deauth after EAPOL key exchange sequence

    wifi无法连接公司的网络 Warning Error in Event Log - deauth after EAPOL key exchange sequence https://forums.i ...

  8. QT中用QStettings生成INI文件来记录QFileDialog::getOpenFileName上次的打开路径

    QSettings setting("./Setting.ini", QSettings::IniFormat); //QSettings能记录一些程序中的信息,下次再打开时可以读 ...

  9. 实用|从0到1 搭建Web性能监控系统

    工具介绍 1. Statsd 是一个使用Node开发网络守护进程,它的特点是通过UDP(性能好,及时挂了也不影响主服务)或者TCP来监听各种数据信息,然后发送聚合数据到后端服务进行处理. 常见支持的「 ...

  10. 记录java

    1.从今天起,我会将自己在java学习道路上的一些心得体会记录下来.