Leetcode979 : Distribute Coins in Binary Tree 二叉树均匀分配硬币问题
问题
给定一个二叉树的root节点,二叉树中每个节点有node.val个coins,一种有N coins。 现在要求移动节点中的coins 使得二叉树最终每个节点的coins value都为1。每次移动,我们只能向相邻接点移动,也就是说coins 只能向父节点或者子节点移动,那么求最终需要移动多少步。
Leetcode原题链接: https://leetcode.com/problems/distribute-coins-in-binary-tree/
分析
如果叶子节点有0个coins,那么我们需要从parent节点移一个coin到叶子节点 (push (-1) 个coin from left node); ----move一次
如果说叶子节点有四个节点那么我们则需要push三个coins到父节点(push +3 coins from leaf node)。----move 三次
定义一个dfs(node)函数,用于表示需要从当前节点push 到its parent节点的coins个数(换句话说就是以当前节点为root的子树中的硬币总数 减去当前总数节点的个数),那么dfs(node) = dfs(node.left) + dfs(node.right) - 1 (减去一个1是因为留一个coin在当前节点) ,而使得以当前节点为root节点需要移动的步数等于abs(dfs(node.left)) + abs(dfs(node.right))。
实现
private int ans = 0;
public int distributeCoins(TreeNode root) {
ans = 0;
distributeCoinsHelper(root);
return ans;
}
private int dfs(TreeNode cur){
if(cur == null){
return 0;
}
int left = dfs(cur.left);
int right = dfs(cur.right);
ans += Math.abs(left) + Math.abs(right);
return left + right + cur.val - 1;
}
Leetcode979 : 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 ...
- [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 ...
- 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 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 ...
- 二叉树分派硬币 Distribute Coins in Binary Tree
2019-03-27 15:53:38 问题描述: 问题求解: 很有意思的题目.充分体现了二叉树的自底向上的递归思路. 自底向上进行运算,对于最底层的二叉子树,我们需要计算每个节点向其parent传送 ...
- Leetcode 110 Balanced Binary Tree 二叉树
判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [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 ...
随机推荐
- centos 7 MysSQL 5.6.39 源码安装
MySQL 5.6.39 二进制安装 CentOS 7 将默认数据库MySQL替换成了Mariadb. 这里会从系统的环境准备开始一步一步安装. 环境准备 系统版本 内核版本 IP地址 Centos ...
- java String比较,“==”和“equal”区别
public static void main(String[] args){ String str1 = new String("str"); String str2 = new ...
- windows下PyCharm安装及使用 【转自 https://blog.csdn.net/yctjin/article/details/70307933?locationNum=11&fps=1】
一.首先安装pycharm,可以参考这篇文章:http://www.jianshu.com/p/042324342bf4 搭建环境 1.win10_X64,其他Win版本也可以.2.PyCharm版本 ...
- mysql那些事(2)时间类型数据如何存储
几乎每次数据库建模的时候,都会遇到时间类型数据存储的问题. mysql存储时间通常选择这四种类型:datetime.timestamp.int和bigint四种方式,到底使用什么类型,需要看具体的业务 ...
- Python一秒搭建ftp服务器,帮助你在局域网共享文件【华为云技术分享】
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/devcloud/article/detai ...
- “洞察千里”,华为云HiLens如何让无人车智行天下
作者:华为云 Rosie 随着人工智能的普及和渗透,"无人"的场景越来越丰富,无人超市.无人车.无人机等已经融入我们的生活. 乘着这股热浪,华为云携手上海交通大学学生创新中心举办了 ...
- 转化欧拉回路(难题)-UESTC-1957励志的猴子
励志的猴子 Time Limit: 1000 MS Memory Limit: 256 MB Submit Status 上图是2013南京亚青会吉祥物圆圆.自从它诞生以来,它就被吐槽为最丑吉 ...
- Balls in the Boxes
Description Mr. Mindless has many balls and many boxes,he wants to put all the balls into some of th ...
- 【搞定Jvm面试】 Java 内存区域揭秘附常见面试题解析
本文已经收录自笔者开源的 JavaGuide: https://github.com/Snailclimb ([Java学习+面试指南] 一份涵盖大部分Java程序员所需要掌握的核心知识)如果觉得不错 ...
- 使用iCamera 测试MT9M001 130w高分辨率摄像头说明
该摄像头默认分辨率为1280*1024,即不设置任何寄存器参数,只要给该模块提供时钟,就可以输出. 在这里 我们可以通过右侧寄存器栏动态调整各寄存器 观察效果. 0x09寄存器可以调整曝光值,可以根据 ...