1022. Sum of Root To Leaf Binary Numbers /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { int sum; public int sumRootToLeaf(TreeNode root) { helper(root,0); return sum; } public void helper(TreeNode root,int num){ if(root==null){ return; } //num=num*2+root.val; helper(root.left,num); helper(root.right,num); if(root.left==null&&root.right==null){ sum=sum+num; } } }