# 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; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up