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的更多相关文章

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

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

  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. Leetcode979 : Distribute Coins in Binary Tree 二叉树均匀分配硬币问题

    问题 给定一个二叉树的root节点,二叉树中每个节点有node.val个coins,一种有N coins. 现在要求移动节点中的coins 使得二叉树最终每个节点的coins value都为1.每次移 ...

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

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

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

随机推荐

  1. java 编写函数将字符串的首尾空格删除。

    String 类有个方法去除字符串首位空格: str.trim(); 查看源代码: public String trim() { int len = value.length; ; char[] va ...

  2. Luogu 1071 - 潜伏者 - [字符串]

    题目链接:https://www.luogu.org/problemnew/show/P1071 题解: 模拟就完事儿了. 注意failed的情况有:出现一个 $f[x]$ 对应多个值:存在两个不同的 ...

  3. [redis] 与redis cluster有关的学习笔记

    主要是以下三个官方文档,只略读了前两个,第三个还没有读. <redis cluster tutorial> <redis sentinel> <redis cluster ...

  4. pandas 2

    ============== sdf={'rkey':[1,2,3,2],'name':['rkey1','rkey2','rkey3','rkey4']}sdf2={'lkey':[1,2,3],' ...

  5. Linux 从源码编译安装 OpenSSH

    https://blog.csdn.net/bytxl/article/details/46639073 Linux 从源码编译安装 OpenSSH以及各问题解决 2015年06月25日 17:37: ...

  6. C++ 在继承中虚函数、纯虚函数、普通函数,三者的区别

    1.虚函数(impure virtual) C++的虚函数主要作用是“运行时多态”,父类中提供虚函数的实现,为子类提供默认的函数实现. 子类可以重写父类的虚函数实现子类的特殊化. 如下就是一个父类中的 ...

  7. JAVA spring 常用包作用详解(转)

    转载地址:https://www.cnblogs.com/Tmc-Blog/p/6093162.html <project xmlns="http://maven.apache.org ...

  8. python 模块大全

    logging   time datetime   sys   os   json  random   hashlib   paramiko  pymysql模块使用 subprocess  pywi ...

  9. selenium操作浏览器

    import org.openqa.selenium.WebDriver; import common.StartFireFox; public class TestBrowser { public ...

  10. [pat]1068 Find More Coins

    满背包问题,把体积和价值看成相等的.用滚动数组优化,然后额外开辟一个choice数组来记录每次的选择,然后回溯打印.因为要按字典序,先把价值进行排序.假如选最小的商品能装满m的话,那就把判断条件改成大 ...