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. 1.JavaWeb 知识点概览

    1.tomcat服务器的安装和配置.http协议 1.1 虚拟目录的 /*映射*/(配置Context元素)(server.xml catalina\localhost\) http://blog.c ...

  2. Oracle使用基础

    1.Oracle的基本概念: 数据库:存储数据的数据库,Oracle一般只有一个全局数据库 XE,ORCL. EX:Express Edition 速成版 ORCL:企业版 SID:SID是Syste ...

  3. Spring Data JPA 大纲归纳

    第一天: springdatajpa day1:orm思想和hibernate以及jpa的概述和jpa的基本操作 day2:springdatajpa的运行原理以及基本操作 day3:多表操作,复杂查 ...

  4. Win10 通过附加进程调试时出现“此任务要求应用程序具有提升的权限”

    最近有新人在使用vs调试时出现了“此任务要求应用程序具有提升的权限”的提示,每次调试vs就会重启一次. 问到我时,我经过查了一番资料才给解决掉了. 其实,问题主要是因为直接启动vs项目时没有足够的权限 ...

  5. 【leetcode】617. Merge Two Binary Trees

    原题 Given two binary trees and imagine that when you put one of them to cover the other, some nodes o ...

  6. 工作中常用的Git操作

    粘贴自:微信公众号:程序员共成长 分支操作: git branch 创建分支 git branch -b 创建并切换到新建的分支上 git checkout 切换分支 git branch 查看分支列 ...

  7. 01 js数据类型

    1.不管什么语言,上来就应该是数据类型了.js也不例外.那么基本的数据类型我们有,boolean, number, string, null, undefine, symbol, object, fu ...

  8. Codeforces 1149 B - Three Religions

    B - Three Religions 思路:dp dp[i][j][k]:a的前i个和b的前j个和c的前k个能构成的最前面的位置 删字符时状态不用改变,加字符时只会改变1*250*250个状态 代码 ...

  9. Python 去除字符串中的空行

    Python 去除字符串中的空行 mystr = 'adfa\n\n\ndsfsf' print("".join([s for s in mystr.splitlines(True ...

  10. 【测试代码执行时间】console.time + console.timeEnd 打印输出耗时

    // 计时开始,内部文字为计时ID,开始要和结束保持一致 console.time('计时器1') // 需要测试执行时间的代码 for (let index = 0; index < 1000 ...