二叉树分派硬币 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 ...
随机推荐
- 简单理解php深复制浅复制问题
其实接触深复制浅复制是通过学习c++了解到的,比如c++很好用的模板,php是不允许方法模板和类模板 一个简单的例子,如果不是很了解php 的取地址符&,可以去看下官方文档,php的& ...
- Codeforces 483 - A/B/C/D/E - (Done)
题目链接:http://codeforces.com/contest/483 A - Counterexample - [简单构造题] Your friend has recently learned ...
- Java学习:注解,反射,动态编译
狂神声明 : 文章均为自己的学习笔记 , 转载一定注明出处 ; 编辑不易 , 防君子不防小人~共勉 ! Java学习:注解,反射,动态编译 Annotation 注解 什么是注解 ? Annotat ...
- spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)
1. 声明式事务管理分类 声明式事务管理也有两种常用的方式, 一种是基于tx和aop名字空间的xml配置文件,另一种就是基于@Transactional注解. 显然基于注解的方式更简单易用,更清爽. ...
- python 当前时间多加一天、一小时、一分钟
datetime模块 import datetime # 获取当前时间 print(datetime.datetime.now()) # 2017-07-15 15:01:24.619000 # 格式 ...
- Python Built-in Function 学习笔记
Python Built-in Function 学习笔记 1. 匿名函数 1.1 什么是匿名函数 python允许使用lambda来创建一个匿名函数,匿名是因为他不需要以标准的方式来声明,比如def ...
- Python tesserocr模块使用示例
操作系统:Win10 1709 X64 python版本:3.6.5 依赖模块:PIL.tesserocr. 需要说明的是,在windows系统上PowerShell通过PIP3 install t ...
- 六、APP开发的主角——UIViewController
MVC框架模式 MVC即model(模型).view(视图)和controller(控制器)的缩写,是一种软件设计模式,专用于含有图形化用户界面的软件设计,自20世纪80年代以来已经有30多年的历史了 ...
- Asynchronous Programming
https://msdn.microsoft.com/zh-cn/library/dd997423.aspx http://www.cnblogs.com/luminji/archive/2010/0 ...
- django-ajax post与get请求
客户端 访问 服务器 方式: 地址栏 get a标签 get form表单 get/post ajax ...