From c6ee62159e405147c53aac68ce75bf08b09f76d8 Mon Sep 17 00:00:00 2001 From: Qiming Xu <458173774@qq.com> Date: Wed, 7 Feb 2024 00:31:09 +0000 Subject: [PATCH] leetcode: finished #1022 --- content/leetcode/2024/2.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/content/leetcode/2024/2.md b/content/leetcode/2024/2.md index 317beea9..0c4d7d48 100644 --- a/content/leetcode/2024/2.md +++ b/content/leetcode/2024/2.md @@ -3,6 +3,38 @@ title: 2024.2 draft: false --- +# 2024.2.7 + +```python +# +# @lc app=leetcode.cn id=1022 lang=python3 +# +# [1022] 从根到叶的二进制数之和 +# + +# @lc code=start +# 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 sumRootToLeaf(self, root: Optional[TreeNode]) -> int: + def dfs(node, val): + if not node: + return 0 + val = val * 2 + node.val + if not node.left and not node.right: + return val + return dfs(node.left, val) + dfs(node.right, val) + + return dfs(root, 0) + + +# @lc code=end +``` + # 2024.2.6 ```python