LeetCode 979. Distribute Coins in Binary Tree
原题链接在这里:https://leetcode.com/problems/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.
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<= N <= 100
0 <= node.val <= N
题解:
Count how many coins current node could give back to its parent.
It could be positive, means having extra coins. Or negative, means needing support from parent. This is the move number, add the absolute value back to result.
The current node, get the count from left child, and count from right right. current node's value plus counts from both left child and right child - 1 is the count that how many coins it could give back to its parent.
Time Complexity: O(n).
Space: O(h).
AC Java:
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- class Solution {
- int res = 0;
- public int distributeCoins(TreeNode root) {
- if(root == null){
- return 0;
- }
- sendCount(root);
- return res;
- }
- private int sendCount(TreeNode root){
- if(root == null){
- return 0;
- }
- int left = sendCount(root.left);
- int right = sendCount(root.right);
- int count = root.val + left + right - 1;
- res += Math.abs(count);
- return count;
- }
- }
LeetCode 979. Distribute Coins in Binary Tree的更多相关文章
- 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 ...
- 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【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 ...
- [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传送 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- sizeof运算符和strlen()函数
首先放上代码和运行结果.(在VC6.0上运行) #include<stdio.h> #include<string.h> int main(void) { char s1[]= ...
- Go基础编程实践(七)—— 并发
同时运行多个函数 观察常规代码和并发代码的输出顺序. // 常规代码,顺序执行,依次输出 package main import ( "fmt" "time" ...
- CLRS10.1-7练习 - 用双队列实现栈
算法中心思想: 始终向非空队列进行入队操作 初始化时两个队列都为空,我们对q1进行入队操作 入栈: 只需执行其中一个队列入队操作即可, 具体操作哪一个队列,用一个标记变量标记 出栈流程图 代码实现 p ...
- vue中sessionStorage的使用
转载:https://www.cnblogs.com/denken/p/11197612.html localStorage 和 sessionStorage 属性允许在浏览器中存储 key/valu ...
- golang 之 sql
golang提供了sql包查询数据 建立连接 导入第三方包 import( "database/sql" _"github.com/go-sql-driver/mysql ...
- 分布式缓存重建并发冲突和zookeeper分布式锁解决方案
如果缓存服务在本地的ehcache中都读取不到数据. 这个时候就意味着,需要重新到源头的服务中去拉去数据,拉取到数据之后,赶紧先给nginx的请求返回,同时将数据写入ehcache和redis中 分布 ...
- pandas-13 时间序列操作方法pd.date_range()
pandas-13 时间序列操作方法pd.date_range() 在pandas中拥有强大的时间序列操作方法. 使用 pd.date_range() 生成 'pandas.core.indexes. ...
- FIneUICore 版本的 AppBoxMvcCore
http://www.51aspx.com/code/codename/64088 CORE版本的APPBOXMVC欢迎下载
- httpclient工具类,post请求发送json字符串参数,中文乱码处理
在使用httpclient发送post请求的时候,接收端中文乱码问题解决. 正文: 我们都知道,一般情况下使用post请求是不会出现中文乱码的.可是在使用httpclient发送post请求报文含中文 ...
- day 06 预科
目录 if判断 if判断习题 for循环 for循环习题 微信机器人 if判断 # 一条狗朝你过来了,你会干吗? 判断: 如果狗是大长腿牵来的狗--->打招呼:如果是条疯狗,跑. # if:如果 ...