问题

给定一个二叉树的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 二叉树均匀分配硬币问题的更多相关文章

  1. 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 ...

  2. [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 ...

  3. 【LeetCode】979. Distribute Coins in Binary Tree 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  4. LeetCode 979. Distribute Coins in Binary Tree

    原题链接在这里:https://leetcode.com/problems/distribute-coins-in-binary-tree/ 题目: Given the root of a binar ...

  5. 【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 ...

  6. 二叉树分派硬币 Distribute Coins in Binary Tree

    2019-03-27 15:53:38 问题描述: 问题求解: 很有意思的题目.充分体现了二叉树的自底向上的递归思路. 自底向上进行运算,对于最底层的二叉子树,我们需要计算每个节点向其parent传送 ...

  7. Leetcode 110 Balanced Binary Tree 二叉树

    判断一棵树是否是平衡树,即左右子树的深度相差不超过1. 我们可以回顾下depth函数其实是Leetcode 104 Maximum Depth of Binary Tree 二叉树 /** * Def ...

  8. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  9. [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 ...

随机推荐

  1. 【Android - 控件】之V - Toolbar的使用

    Toolbar是Android V7包中的一个控件,用来代替Action Bar作为界面的头部标题栏布局.Toolbar相对于Action Bar的特点是更加灵活,可以显示在任何位置. 首先先来看To ...

  2. 少用float浮动?

    在css中,float 属性定义元素在哪个方向浮动.也是我在css样式中常用到的属性,后来浏览了一些公司项目代码,发现float属性极少有人使用.随后做了一些调查和研究: 1.在ie6以下,float ...

  3. rsync工具、rsync常用选项、以及rsync通过ssh同步 使用介绍

    第8周5月14日任务 课程内容: 10.28 rsync工具介绍10.29/10.30 rsync常用选项10.31 rsync通过ssh同步 10.28 rsync工具介绍 rsync是一个同步的工 ...

  4. JS&jQuery

    1.JavaScript概述    1.什么是JavaScript        JavaScript简称JS,是一种专门运行于JS解释器/引擎中的解释型脚本语言    2.JS发展史         ...

  5. matlab 降维工具 转载【https://blog.csdn.net/tarim/article/details/51253536】

    降维工具箱drtool   这个工具箱的主页如下,现在的最新版本是2013.3.21更新,版本v0.8.1b http://homepage.tudelft.nl/19j49/Matlab_Toolb ...

  6. 十、Spring boot 简单优雅的整合 Swagger2

    前言 swagger2 是什么,我这里就不说了,就是一个简单的接口文档,方便前后端联调. 其实之前没有想要到要使用swagger 的.因为我之前用的是YAPI ,不过这个是一个单独的工具.并且是开源的 ...

  7. Delphi7 - Server Monitor开发并实现指定端口定时刷新、重启和邮件提醒等功能

    项目背景 近期,总经办邮件反馈考勤数据频繁丢失,请IT排查其根本原因,并提供整改措施. 措不及防,这个项目当初并不是IT主导的,是设备部采购,然后协同软件供应商直接安装.部署和调试的,IT只是提供几个 ...

  8. Spring Boot 最简单整合Shiro+JWT方式

    简介 目前RESTful大多都采用JWT来做授权校验,在Spring Boot 中可以采用Shiro和JWT来做简单的权限以及认证验证,在和Spring Boot集成的过程中碰到了不少坑.便结合自身以 ...

  9. 华为云ModelArts图深度学习,学习知识还能考取微认证

    作为人工智能最前沿的技术之一,图深度学习被公认是人工智能认识世界实现因果推理的关键,也是深度学习未来发展的方向.但深度学习对图数据模型的支持性差一直是众多研究者难以攻克的难点,因此图深度学习在实际生产 ...

  10. Pandas学习(二)——双色球开奖数据分析

    学习笔记汇总 Pandas学习(一)–数据的导入 pandas学习(二)–双色球数据分析 pandas学习(三)–NAB球员薪资分析 pandas学习(四)–数据的归一化 pandas学习(五)–pa ...