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.每次移 ...
随机推荐
- Windows----Github环境搭建
关于Github的那点事: 一.下载安装 下载地址: https://git-for-windows.github.io/ 在官方下载完后,安装到Windows Explorer integra ...
- html 刷新重载方法汇总
一.javascript页面刷新重载的方法: <a href="javascript:location.reload();">点击重新载入页面</a> &l ...
- L2-023. 图着色问题*
L2-023. 图着色问题 参考博客 #include <iostream> #include <cstring> #include <set> using nam ...
- Mysql 复制一个新表
1.复制表结构及数据到新表CREATE TABLE 新表 SELECT * FROM 旧表这种方法会将oldtable中所有的内容都拷贝过来,当然我们可以用delete from newtable;来 ...
- Windows 应用商店无法下载---启动更新
今天想在应用商店下载东西,但是以直没成功,查看原因结果是因为我的Windows自动更新关了. 百度,如何打开自动更新,要打开本地组策略编辑器,但是我是Windows家庭版,,,没有这个东西,, 最后, ...
- SpringCloud分布式配置中心所遇问题
1.如果更换了获取配置文件的名称一定要在客户端配置文件的name更改2.不建议服务器设置下拉到git仓库的配置文件地址,可能导致更换地址之后无法获取到新更改的
- c——动态数组
动态数组的实现 #include<stdio.h> #include<stdlib.h> int main(){ int i,n,*a; scanf("%d" ...
- PythonStudy——三种字符串 Three strings
# 普通字符串:u'以字符作为输出单位'print(u'abc') # 用于显示 # 二进制字符串:b'' 二进制字符串以字节作为输出单位print(b'abc') # 用于传输 # 原义字符串:r' ...
- 登录ssh提示:ssh_exchange_identification: read: Connection reset by peer error
vim /etc/hosts.allow 添加 sshd : ALL
- 第3章 Java数组(上): 一维数组和二维数组
3.数组及排序算法(2天) 3.1 数组的概述 2课时 3.2 一维数组的使用 3课时 3.3 多维数组的使用 3课时 3.4 数组中涉及到的常见算法 3课时 3.5 Arrays工具类的使用 3课时 ...