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 <= 1000 <= 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 ...
随机推荐
- 【转】python测试框架--doctest
转自https://my.oschina.net/lionets/blog/268542 doctest 是一个 Python 发行版自带的标准模块.有两种模式可供使用. ##1 doctest 的概 ...
- Fiddler抓包8-打断点(bpu)(转)
转自:https://www.cnblogs.com/yoyoketang/p/6778006.html
- Js的iframe相关问题,如何判断当前是否在iframe中,iframe和它的父窗口如何通信
一.前言: 在web中,为了丰富我们的内容,往往需要引用其它HTML文件,这时候就需要用到 iframe 标签,本文就主要记录一下使用iframe所需要注意的问题 iframe 所使用的环境(笔者所遇 ...
- selenium自学笔记---下拉框定位元素select
下拉框1.先定位select 然后在定位option city = driver.find_element_by_id("selCities_0") city.find_eleme ...
- HTTP专业术语,你了解多少?
HTTP协议是什么? 超文本传输协议(HTTP)是一种为分布式.协作式的,面向应用层的超媒体信息系统.它是一种通用的.无状态(stateless)的协议,除了应用于超文本传输外,它也可以应用于如名称服 ...
- hadoop细节 -> 持续更新
Hdfs: hdfs写流程: 客户端通过DistributedFileSystem请求namenode上传文件 Namenode进行检查,比如父路径 文件本身,是否允许上传 Namenode相应信 ...
- linux入门—安装linux系统(1)
一,linux介绍 linux是一套免费使用和自由传播的类Unix操作系统,简单的说就是不要钱,你可以随便使用,也可以分享给其他人. (剩下的详细内容,个人认为百度百科的内容比我瞎讲强的多,网址:ht ...
- php正则表达式中preg_match_all函数的详解
php正则表达式中的函数我们之前为大家结果一个preg_match函数,相信大伙对此有所了解,那么php正则表达式中preg_match_all函数的具体使用是如何的呢?今天我们就带大家了解php正则 ...
- Tensorflow替换静态图中的OP
import tensorflow as tf import collections from tensorflow.core.framework import tensor_shape_pb2 # ...
- laravel项目中通过nvmw安装node.js和npm 开发环境-- windows版
windows版本安装 此教程执行的时候,网速一定要好.不然可能出现各种错误. 如果本文对你有用,请爱心点个赞,提高排名,帮助更多的人.谢谢大家!❤ git clone nvmw 直接从 githu ...