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 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的更多相关文章
- LeetCode 979. Distribute Coins in Binary Tree
原题链接在这里:https://leetcode.com/problems/distribute-coins-in-binary-tree/ 题目: Given the root of a binar ...
- 【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 ...
- 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- [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 ...
- Leetcode979 : Distribute Coins in Binary Tree 二叉树均匀分配硬币问题
问题 给定一个二叉树的root节点,二叉树中每个节点有node.val个coins,一种有N coins. 现在要求移动节点中的coins 使得二叉树最终每个节点的coins value都为1.每次移 ...
- 二叉树分派硬币 Distribute Coins in Binary Tree
2019-03-27 15:53:38 问题描述: 问题求解: 很有意思的题目.充分体现了二叉树的自底向上的递归思路. 自底向上进行运算,对于最底层的二叉子树,我们需要计算每个节点向其parent传送 ...
- LC 297 Serialize and Deserialize Binary Tree
问题: Serialize and Deserialize Binary Tree 描述: Serialization is the process of converting a data stru ...
- 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 ...
- [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 ...
随机推荐
- RocketMQ顺序消息
rocketmq的顺序消息需要满足2点: 1.Producer端保证发送消息有序,且发送到同一个队列.2.consumer端保证消费同一个队列. 生产端: RocketMQ可以严格的保证消息有序.但这 ...
- Vim技巧----选取一个单词
viw 它的作用是选取一个单词(word),无论光标在这个单词的哪个位置都能选中整个单词. 每日一Vim(18)Text-Object 前两节讲了Visual mode相关内容,这里提一个小问题,“如 ...
- Files的常用方法都有哪些?(未完成)
Files的常用方法都有哪些?(未完成)
- [唐胡璐]Selenium技巧- IE浏览器Zoom和Protected Model Setting
Selenium Webdriver在IE下跑脚本的时候要保证页面大小为100%,且要在IE internet options, selectSecurity tab and uncheck “Ena ...
- macOS上更顺手的终端
安装iTerm2.下载地址 https://iterm2.com/downloads/stable/latest 安装Nerd Fonts.下载地址 https://github.com/ryanoa ...
- Unrecognized SSL message, plaintext connection? 将https 换为http 即可
请求链接:https://59********* 升级后的项目地址有https换为了http ,出现这个错误,改为http请求即可
- Idea使用Lombok简化实体类代码
引入相应的maven包 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lo ...
- MSMQ使用
Message Message是MSMQ的数据存储单元,我们的用户数据一般也被填充在Message的body当中,因此很重要,让我们来看一看其在.net中的体现,如图: 在图上我们可以看见,Messa ...
- 物联网是前端工程师的新蓝海吗? | Live笔记
物联网是继 Web .无线之后的又一次重大技术变革,在变革的大潮中,程序员的知识体系和思维方式将面临全面更新. 前端开发的历史 在准备这个live的过程中,我回顾了前端开发短暂的历史,有几次我认为非常 ...
- C++虚函数和成员函数内存 this指针问题
father* p=new son; p->disp(...); 发现有好多人this指针的本质有错误的认识,估计不少人一说起this指针,脑袋立即反应出:那个类里使用的this指针,那么这个指 ...