二叉树分派硬币 Distribute Coins in Binary Tree
2019-03-27 15:53:38
问题描述:


问题求解:
很有意思的题目。充分体现了二叉树的自底向上的递归思路。
自底向上进行运算,对于最底层的二叉子树,我们需要计算每个节点向其parent传送多余的硬币数量,不论正负,都是需要占用move数量的。自此递归的进行计数即可。

public int distributeCoins(TreeNode root) {
int[] res = new int[1];
helper(root, res);
return res[0];
}
private int helper(TreeNode root, int[] res) {
if (root == null) return 0;
int l = helper(root.left, res);
int r = helper(root.right, res);
res[0] += Math.abs(l) + Math.abs(r);
return l + r + root.val - 1;
}
二叉树分派硬币 Distribute Coins in Binary Tree的更多相关文章
- [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 ...
- 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
原题链接在这里: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 ...
- Leetcode979 : Distribute Coins in Binary Tree 二叉树均匀分配硬币问题
问题 给定一个二叉树的root节点,二叉树中每个节点有node.val个coins,一种有N coins. 现在要求移动节点中的coins 使得二叉树最终每个节点的coins value都为1.每次移 ...
- [Swift]LeetCode103. 二叉树的锯齿形层次遍历 | Binary Tree Zigzag Level Order Traversal
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...
- [Swift]LeetCode104. 二叉树的最大深度 | Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [Swift]LeetCode124. 二叉树中的最大路径和 | Binary Tree Maximum Path Sum
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
随机推荐
- PAT甲级1068 Find More Coins【01背包】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 题意: n个硬币,每一个有一个特有的价 ...
- 记一次windows服务开发中遇到的问题
最近在研究windows service和quartz.net,所以迅速在园子大神那里扒了一个demo,运行,安装一切顺利. 但在在App.config配置中增加了数据库连接字符串配置后,服务安装后无 ...
- day21:包和异常处理
1,复习 # 序列化模块 # json # dumps # loads # dump 和文件有关 # load load不能load多次 # pickle # 方法和json的一样 # dump和lo ...
- C++中的const成员函数(函数声明后加const,或称常量成员函数)用法详解
http://blog.csdn.net/gmstart/article/details/7046140 在C++的类定义里面,可以看到类似下面的定义: 01 class List { 02 priv ...
- sql中遍历字符串
在sql或者存储过程中会需要遍历字符串. ), --如111,222,333,尾部加, ), @Id int, ) set @split = ',' ) begin ,) ,charindex(@sp ...
- 【UML】NO.54.EBook.6.UML.2.002-【Thinking In UML 大象 第二版】- UML 核心元素
1.0.0 Summary Tittle:[UML]NO.54.EBook.6.UML.2.002-[Thinking In UML 大象 第二版]- UML 核心元素 Style:DesignPat ...
- 关于npm Vue
参考:http://www.runoob.com/w3cnote/vue2-start-coding.html 安装vue脚手架 npm install vue-cli -g 查看当前脚手架版本 np ...
- LEFT JOIN、RIGHT JOIN、INNER JOIN、FULL JOIN 使用
select * from t_class_info as c; id gradeid classid year createtime ...
- netframework webapi exceptionless
1.webapi项目 添加nuget exceptionless webapi 2.在exceptionless server端添加项目,注意key 3.修改api项目的webconfig &l ...
- UI框架搭建DAY2
今天的主要任务是完善NormalPanel, 搭建PopupPanel. 在编写PanelManager的过程中,发现了一个bug.昨天把panelPath直接传给了ResourceManager.G ...