---
# System prepended metadata

title: 1022. Sum of Root To Leaf Binary Numbers

---

# 1022. Sum of Root To Leaf Binary Numbers

https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/

# 思路：
1. 2進位數字 向左位移，代表整個數值*2 
所以才會有re =2*res+ root.val 


```python=

# 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 sumRootToLeaf(root, res): # here; "res" is "sum"
            if root == None: return 0
            res = (2 * res) + root.val
            if root.left == None and root.right == None: return res
            return sumRootToLeaf(root.left, res) + sumRootToLeaf(root.right, res)
        
         return sumRootToLeaf(root, 0)
```