leetcode979
搞不定这种递归计算,可能我的头脑是“线性”的,这种一层一层的,想起来太费劲了,想的头发都没了。以后希望能有AI来写这种程序吧,AI不怕掉头发!
class Solution(object):
def __init__(self):
self.res = 0 def dfs(self,root):
if not root:
return 0
left = self.dfs(root.left)
right = self.dfs(root.right)
self.res += abs(left) + abs(right)
return root.val + left + right - 1 def distributeCoins(self, root: TreeNode) -> int:
self.dfs(root)
return self.res
leetcode979的更多相关文章
- [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 ...
- LeetCode979. 在二叉树中分配硬币
问题:979. 在二叉树中分配硬币 给定一个有 N 个结点的二叉树的根结点 root,树中的每个结点上都对应有 node.val 枚硬币,并且总共有 N 枚硬币. 在一次移动中,我们可以选择两个相邻的 ...
- Leetcode979 : Distribute Coins in Binary Tree 二叉树均匀分配硬币问题
问题 给定一个二叉树的root节点,二叉树中每个节点有node.val个coins,一种有N coins. 现在要求移动节点中的coins 使得二叉树最终每个节点的coins value都为1.每次移 ...
随机推荐
- 初始Java
- 使用POI读取xlsx文件,包含对excel中自定义时间格式的处理
package poi; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcepti ...
- java基础(7)集合与泛型
第一部分:Collection集合 1 collection集合概述 集合按照其存储结构可以分为两大类,分别是单列集合java.util.Collection和双列集合java.util.Map. C ...
- python的高阶函数式编程
首先 函数式编程≠函数编程,就跟计算机≠计算,因为计算机基于硬件,计算基于算法,所以函数式编程是倾向于算法. 高阶函数定义: 一个函数接受的这个参数,而这个参数也是一个函数,称之为高阶函数 例如: ...
- go-json处理的问题
1.通过Decoder来解析json串 package main import ( "encoding/json" "fmt" "io" & ...
- [Java] 例外處裡 try/catch & throws
public class CheckException { public static void main(String[] args) { File file = new File("xx ...
- 使用maven-tomcat7-plugins时调试出现source not found解决
直接看下面的步骤: 步骤1: 步骤2: 步骤3: 步骤4:
- 18.3 #define DM9000_DBG(fmt,args...) printf(fmt, ##args)代表什么
标准C支持可变参数的函数,意味着函数的参数是不固定的,例如printf()函数的原型为:int printf( const char *format [, argument]... ) 而在GNU C ...
- nginx+keeplived+tomcat
1,宣告操作系统版本,nginx,java,tomcat,keeplived版本 操作系统 用途 VIP IP地址 软件版本 CentOS 7.3 mini NTP服务器 无 192.168.197. ...
- VS2012 安装 NPOI (管理NuGet程序包)
问题背景 选择项目后右键==>管理NuGet程序包,搜索NPOI,返回服务器无法找到...404 解决方法: 第一步: 访问:https://www.nuget.org/api/v2/ ...