Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins 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.

Runtime: 4 ms, faster than 100.00% of C++ online submissions for Distribute Coins in Binary Tree.

class Solution {
private:
int ret;
public:
int distributeCoins(TreeNode* root) {
ret = ;
helper(root);
return ret;
}
int helper(TreeNode* root) {
if(!root) return ;
int left = helper(root->left), right = helper(root->right);
ret += abs(left) + abs(right);
return left + right + root->val - ;
}
};

LC 979. Distribute Coins in Binary Tree的更多相关文章

  1. LeetCode 979. Distribute Coins in Binary Tree

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

  2. 【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 ...

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

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

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

  5. Leetcode979 : Distribute Coins in Binary Tree 二叉树均匀分配硬币问题

    问题 给定一个二叉树的root节点,二叉树中每个节点有node.val个coins,一种有N coins. 现在要求移动节点中的coins 使得二叉树最终每个节点的coins value都为1.每次移 ...

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

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

  7. LC 297 Serialize and Deserialize Binary Tree

    问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...

  8. LC 662. Maximum Width of Binary Tree

    Given a binary tree, write a function to get the maximum width of the given tree. The width of a tre ...

  9. [LC] 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. PCQQ - 发送自定义的XML卡片消息

    效果: 原理: qq分享产生的xml卡片消息存储在qq内存中,可以在qq运行内存中搜索找到其xml源码,记录源码相应的内存地址,通过内存地址修改掉内存数据,再次转发这条分享的消息就会发现内容的变化. ...

  2. vue-element-admin 之改变登录界面input的光标颜色

    前话:用框架原有的login更改而不重写的话,恰好当你input背景设置成白色的时候,光标会找不到=>原因:原框架的光标颜色是#fff 操作更改光标颜色: 找到src/views/login/i ...

  3. Delphi Tobject类

  4. 009.增删查改语法(sql实例)

    --------------------------------------------合并结果集SELECT UNION -------------------------------------- ...

  5. Redis5.0.3单机版安装

    一.创建redis源码包存放目录 cd /usr/local/ mkdir redis 二.进入创建的目录,下载最新版Redis yum -y install wget wget http://dow ...

  6. 消息中间之ActiveMQ

    一.JMS (JAVA Message Service) 1. JMS基本概念 JMS(JAVA Message Service,java消息服务)是java的消息服务,JMS的客户端之间可以通过JM ...

  7. TensorFlow指定GPU/CPU进行训练和输出devices信息

    TensorFlow指定GPU/CPU进行训练和输出devices信息 1.在tensorflow代码中指定GPU/CPU进行训练 with tf.device('/gpu:0'): .... wit ...

  8. Spring-简介-IOC理论推导

    1.Spring 1.1.简介 Spring:春天----->给软件行业带来了春天! 2002,首次推出了Spring框架的雏形:interface21框架! Spring框架即以interfa ...

  9. 微信小程序审核不通过的解决方法

    前言 近来,微信小程序一直活跃在开发者的眼球中.很多开发者都投身微信小程序的开发中,而这些开发者,总是需要面对最后一道难题:如何以一种优雅的姿势来通过微信官方的审核.本文基于几天前提交审核的一次总结, ...

  10. C# Transaction 事务处理 -环境事务

    一.TransactionScope 环境事务 static async Task TransactionScopeAsync() { using (var scope = new Transacti ...