当前位置:网站首页>[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)
边栏推荐
- 生物素叠氮化物中的(CAS:1527486-16-3TAMRA-azide-PEG3-Biotin)反应的特点!
- 渗透测试与攻防对抗——漏洞扫描&逻辑漏洞(Part1)
- Sikuli 基于图形识别的自动化测试技术
- 对修饰器的实验支持功能在将来的版本中可能更改。在 “tsconfig“ 或 “jsconfig“ 中设置 “experimentalDecorators“ 选项以删除此警告
- DALL·E-2是如何工作的以及部署自己的DALL·E模型
- c语言指针练习题
- Are the numbers entered symmetrical?
- 信息化和数字化的核心差异
- 五种定位——粘性定位
- Minimum number of steps to get out of the maze 2
猜你喜欢
开发IM即时通讯容易吗?需要什么技术
Solidity最强对手:MOVE语言及新公链崛起
嵌入式Qt-实现两个窗口的切换
What do you know about FITC-labeled biotin (FITC-biotin|CAS: 134759-22-1)?
CVPR22 Oral|通过多尺度token聚合分流自注意力,代码已开源
365天挑战LeetCode1000题——Day 052 逐步求和得到正数的最小值 贪心
Pyscript,创建一个能执行crud操作的网页应用
GB28181 sip和RTSP(Real-Time Streaming Protocol)实时流控制协议
使用 GoogleTest 框架对 C 代码进行单元测试
跳房子游戏
随机推荐
信息化和数字化的核心差异
assert利用蚁剑登录
Xi'an biotin-tetrapolyethylene glycol-amide-4phenol light yellow semi-solid
Fedora 36 dnf 安装ModSecurity和 OWASP 核心规则集
What should I do if there is no sound after reinstalling the system in win10?
即时通讯开发如何撸一个WebSocket服务器
跨部门沟通的技巧
JVM :运行时数据区-虚拟机栈
什么是一网统管?终于有人讲明白了
Are the numbers entered symmetrical?
宽带由20M换为100M
Penetration Testing and Offensive and Defense Confrontation - Vulnerability Scanning & Logic Vulnerability (Part1)
ITK编译remote库
20220809-PotPlayer如何设置默认字体色-设置默认字体色的方法
高校就业管理系统设计与实现
无js实现弹出层效果
【CAS:41994-02-9 |Biotinyl tyramide】生物素基酪氨酰胺价格
这一次,话筒给你:向自由软件之父 Richard M. Stallman 提问啦!
-Pickling peanuts-
Data storage - the C language