当前位置:网站首页>[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)
边栏推荐
猜你喜欢
随机推荐
Solidity最强对手:MOVE语言及新公链崛起
破产企业的职工退休怎么办?
How to turn off system protection in Win11?How to turn off the system protection restore function?
地雷数量求解
DALL·E-2是如何工作的以及部署自己的DALL·E模型
shell指定参数名传参
flask——请求、响应、请求扩展、session、闪现、蓝图、g对象、flask-session
【kali-密码攻击】(5.1.2)密码在线破解:Medusa
【Grpc】报错:status = StatusCode.UNIMPLEMENTED details = ““
-Chess game-
Qt的pro文件递归搜寻添加文件
Involved in PEG-Biotin (CAS: 1778736-18-7) Biotin-PEG4-OH is widely used in molecular target detection
Fedora 36 dnf 安装ModSecurity和 OWASP 核心规则集
有PEG-Biotin参与的(CAS:1778736-18-7)Biotin-PEG4-OH广泛用于分子靶点检测
Mysql数据库 ALTER 基本操作
芯片资讯|半导体收入增长预计将放缓至 7%,蓝牙芯片需求依然稳步增长
eyb:Redis学习(4)
FITC标记生物素(FITC-生物素|CAS:134759-22-1)有哪些知识了?
使用 GoogleTest 框架对 C 代码进行单元测试
Penetration Testing and Offensive and Defense Confrontation - Vulnerability Scanning & Logic Vulnerability (Part1)









