---
# System prepended metadata

title: 1022. Sum of Root To Leaf Binary Numbers
tags: [Leetcode, Tree, Easy]

---

# 1022. Sum of Root To Leaf Binary Numbers
###### tags: `Leetcode` `Easy` `Tree`
Link: https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/description/
## Code
```java=
class Solution {
    int ans = 0;
    public int sumRootToLeaf(TreeNode root) {
        if(root==null) return 0;
        dfs(root, 0);
        return ans;
    }
    public void dfs (TreeNode curr, int val){
        val = val*2 + curr.val;
        if(curr.left!=null) dfs(curr.left, val);
        if(curr.right!=null) dfs(curr.right, val);
        if(curr.left==null && curr.right==null) ans+=val;
    }
}
```