Sum Root to Leaf Numbers | LeetCode | Java

RMAG news
class Solution {
int resSum = 0;
public int sumNumbers(TreeNode root) {
getSumRoot(root, 0);
return resSum;
}

void getSumRoot(TreeNode node, int sum){
if(node==null)
return;

sum = sum * 10 + node.val;

if(node.left==null && node.right==null){
resSum += sum;
return;
}

getSumRoot(node.left, sum);
getSumRoot(node.right, sum);
}
}

Thanks for reading 🙂
Feel free to comment and like the post if you found it helpful
Follow for more 🤝 && Happy Coding 🚀

If you enjoy my content, support me by following me on my other socials:
Github
Twitter(X)
Hashnode
Medium