当前位置:网站首页>[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)
边栏推荐
猜你喜欢

【kali-密码攻击】(5.1.2)密码在线破解:Medusa

惊掉你下巴,程序员编码竟然可以被 996 指数化

PEG 衍生物Biotin-PEG1-OH(cas:95611-10-2,2-生物素氨基乙醇)优势说明

assert利用蚁剑登录

什么是一网统管?终于有人讲明白了

基于Web的疫情隔离区订餐系统

02| operator

D-Biotinol Involved by Biotin, CAS No: 53906-36-8 Specific Properties Description

Biotin-Cy2 Conjugate,生物素-Cy2 偶联物_Cy2 生物素偶联物

CAS:851113-28-5 (Biotin-ahx-ahx-tyramine)
随机推荐
CMake 编译运行dpdk项目程序
生物素叠氮化物中的(CAS:1527486-16-3TAMRA-azide-PEG3-Biotin)反应的特点!
XSS高级 svg 复现一个循环问题以及两个循环问题
365天挑战LeetCode1000题——Day 052 逐步求和得到正数的最小值 贪心
惊掉你下巴,程序员编码竟然可以被 996 指数化
破产企业的职工退休怎么办?
-骑士巡游-
egg.js中Class constructor BaseContextClass cannot be invoked without ‘new‘解决方法
嵌入式Qt-实现两个窗口的切换
FITC标记生物素(FITC-生物素|CAS:134759-22-1)有哪些知识了?
Docker interview question 2--get the number of database connections and docker-compose
D-Biotinol Involved by Biotin, CAS No: 53906-36-8 Specific Properties Description
JDBC数据库连接池练习题
微信小程序tab切换时保存checkbox状态
Mysql数据库 ALTER 基本操作
2022金九银十工作潮,怎么样才能成功跳槽面试拿到高薪呢?
五种定位——粘性定位
Unity顶点动画
PEG derivative Biotin-PEG1-OH (cas: 95611-10-2, 2-biotinaminoethanol) advantage description
跨部门沟通的技巧