当前位置:网站首页>【LeetCode】求根节点到叶节点数字之和
【LeetCode】求根节点到叶节点数字之和
2022-08-10 00:39:00 【LawsonAbs】
1.题目

2. 思想
递归
3. 代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def __init__(self):
self.res = 0
def sumNumbers(self, root: Optional[TreeNode]) -> int:
self.dfs(root,tmp=0)
return self.res
# 深搜找出所有叶节点
# tmp 表示根节点到当前节点的值
def dfs(self,root,tmp):
if root is None: # 如果是None,说明到头儿了
return
if root.left:
self.dfs(root.left,tmp*10 + root.val)
if root.right:
self.dfs(root.right,tmp*10 + root.val)
# 如果左右节点都是空,说明当前就是根节点
if root.left ==None and root.right==None:
self.res += (tmp*10 + root.val)
边栏推荐
- 什么是一网统管?终于有人讲明白了
- Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection
- 改变社交与工作状态的即时通讯是什么呢?
- 20220808-一些想法
- How to activate the payment function on WeChat official account?
- Qt的pro文件递归搜寻添加文件
- Docker interview question 2--get the number of database connections and docker-compose
- 你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
- 宝塔实测-搭建LightPicture开源图床系统
- How to turn off system protection in Win11?How to turn off the system protection restore function?
猜你喜欢

assert利用蚁剑登录

商业模式及其 SubDAO 深入研究

【CAS:41994-02-9 |Biotinyl tyramide】Biotinyl tyramide price

UI遍历的初步尝试

生物素叠氮化物中的(CAS:1527486-16-3TAMRA-azide-PEG3-Biotin)反应的特点!

GBJ1510-ASEMI机器人电源整流桥GBJ1510

What do you know about FITC-labeled biotin (FITC-biotin|CAS: 134759-22-1)?

C语言--数据的存储(上)

改变社交与工作状态的即时通讯是什么呢?

Enhanced Deep Residual Networks for Single Image Super-Resolution
随机推荐
Pyscript,创建一个能执行crud操作的网页应用
OSS-访问oss生成的url无法访问,直接下载问题
What should I do if there is no sound after reinstalling the system in win10?
【软考软件评测师】软件测试基础知识
-红与黑-
egg.js中Class constructor BaseContextClass cannot be invoked without ‘new‘解决方法
Next.js获取路由参数及styled-jsx 的使用
宽带由20M换为100M
shell指定参数名传参
y92.第六章 微服务、服务网格及Envoy实战 -- Envoy基础(三)
分析 20 个 veToken 生态系统协议 这种代币模型为何受欢迎?
What do you know about FITC-labeled biotin (FITC-biotin|CAS: 134759-22-1)?
数据建模已死,真的吗?
微信公众号如何开通支付功能?
365天挑战LeetCode1000题——Day 052 逐步求和得到正数的最小值 贪心
【毕业设计】 基于Stm32的家庭智能监控系统 - 单片机 图像识别 人体检测 AI
eyb:Redis学习(4)
JVM :运行时数据区-虚拟机栈
flask——请求、响应、请求扩展、session、闪现、蓝图、g对象、flask-session
CAS:183896-00-6 (Biotin-PEG3-C3-NH2) PEG derivative