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 ...
随机推荐
- 【Git】三、版本回退&撤消修改&文件删除
提要 //查看git操作日志 $ git log //单行格式查看操作日志 $ git log --pretty=oneline //还原操作到上一次版本,有几个^就上几次 $ git reset - ...
- Prometheus 监控 Redis 集群的正确姿势
Prometheus 监控Redis的正确姿势(redis集群) Prometheus 监控 Redis cluster,其实套路都是一样的,使用 exporter. exporter 负责采集指标, ...
- 解决Ubuntu下ssh无法启动
Ubuntu ssh一直无法启动. 通过 systemctl status ssh.service 查看到的错误是 Dec 16 13:35:22 iZm5eckxl2tqyka9eoe7b3Z ...
- Go测试开发就用这三板斧
一个古老的面试问题:“给你个XX,你怎么测试?” 时间穿越到9102,Go语言成为了新生代的代名词.老问题变成了“给你Golang程序,你怎么测试?” 看完本文后,读者可以拍着胸脯回答,“一共 ...
- CentOS7 解决不能切换中英文输入法的问题
1. 运行 im-chooser(如果没有要先安装) $ im-chooser 2. 在打开的窗口选择 iBus,然后 Logout 再 Login, 输入法即可切换.
- javascript只弹出一次框 再次刷新不弹出
.打开页面自动弹出 当关闭弹框的时候 设置cookie生存时间 再次刷新页面判断cookie是否失效 <html> <head> <meta charset=&qu ...
- Go语言关于Type Assertions的疑问
我在"The Go Programming Language Specification"中读到了关于x.(T)这样的语法可以对变量是否符合某一type或interface进行判断 ...
- 前端自动化构建工具 Webpack——3 webpack配置文件的使用
- P1198最大数——线段树点修改&&模板题
题目 题目链接 大意:维护一个数列,有两种操作: 查询操作Q L:查询当前数列中末尾L个数中的最大的数 插入操作A n:将n加上t再对D取模,将所得值插入数列末尾 解决方案 由题意知,只有两种操作 ...
- macOS上更顺手的终端
安装iTerm2.下载地址 https://iterm2.com/downloads/stable/latest 安装Nerd Fonts.下载地址 https://github.com/ryanoa ...