当前位置:网站首页>[LeetCode] Find the sum of the numbers from the root node to the leaf node
[LeetCode] Find the sum of the numbers from the root node to the leaf node
2022-08-10 02:11: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
# A deep search finds all leaf nodes
# tmp Represents the value from the root node to the current node
def dfs(self,root,tmp):
if root is None: # 如果是None,Explanation is over
return
if root.left:
self.dfs(root.left,tmp*10 + root.val)
if root.right:
self.dfs(root.right,tmp*10 + root.val)
# If left and right nodes are empty,Indicates that it is currently the root node
if root.left ==None and root.right==None:
self.res += (tmp*10 + root.val)
边栏推荐
- Pagoda measurement - building LightPicture open source map bed system
- 【LeetCode】求根节点到叶节点数字之和
- ITK编译remote库
- FITC标记生物素(FITC-生物素|CAS:134759-22-1)有哪些知识了?
- 【毕业设计】基于ESP32的在线墨水屏桌面摆件 -物联网 单片机 嵌入式
- CAS:851113-28-5 (Biotin-ahx-ahx-tyramine)
- 你有对象类,我有结构体,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang结构体(struct)的使用EP06
- 使用 GoogleTest 框架对 C 代码进行单元测试
- 【毕业设计】 基于Stm32的家庭智能监控系统 - 单片机 图像识别 人体检测 AI
- egg.js中Class constructor BaseContextClass cannot be invoked without ‘new‘解决方法
猜你喜欢
由生物素参与的D-Biotinol,CAS号:53906-36-8具体特性说明
商业模式及其 SubDAO 深入研究
Aptos 深度解读:机遇、挑战与风险
ASEMI整流桥GBJ1010参数,GBJ1010规格,GBJ1010封装
【CAS:41994-02-9 |Biotinyl tyramide】生物素基酪氨酰胺价格
03|Process Control
365天挑战LeetCode1000题——Day 052 逐步求和得到正数的最小值 贪心
How to turn off system protection in Win11?How to turn off the system protection restore function?
小程序中计算距离信息
Docker 面试题2则--取数据库连接数和docker-compose
随机推荐
Data storage - the C language
2022金九银十工作潮,怎么样才能成功跳槽面试拿到高薪呢?
罗彻斯特大学 | 现在是什么序列?蛋白质序列的贝叶斯优化的预训练集成
ITK编译remote库
03|流程控制
嵌入式Qt-实现两个窗口的切换
什么是 PWA
el-input保留一位小数点
跨部门沟通的技巧
CAS:851113-28-5 (生物素-ahx-ahx-酪胺)
MySQL最大连接数限制如何修改
win10重装系统后没声音怎么办?
oracle的数据导入导出
由生物素参与的D-Biotinol,CAS号:53906-36-8具体特性说明
Solving for the number of mines
y92.第六章 微服务、服务网格及Envoy实战 -- Envoy基础(三)
GBJ1510-ASEMI机器人电源整流桥GBJ1510
删除表空间数据文件
JVM :运行时数据区-虚拟机栈
Character Statistics Histogram