Sum of Path Numbers (medium)
Problem Statement
Given a binary tree where each node can only have a digit (0-9) value, each root-to-leaf path will represent a number. Find the total sum of all the numbers represented by all paths.
Try it yourself
Try solving this question here:
import java.util.*;class TreeNode {int val;TreeNode left;TreeNode right;TreeNode(int x) {val = x;}};class SumOfPathNumbers {public static int findSumOfPathNumbers(TreeNode root) {// TODO: Write your code herereturn -1;}public static void main(String[] args) {TreeNode root = new TreeNode(1);root.left = new TreeNode(0);root.right = new TreeNode(1);root.left.left = new TreeNode(1);root.right.left = new TreeNode(6);root.right.right = new TreeNode(5);System.out.println("Total Sum of Path Numbers: " + SumOfPathNumbers.findSumOfPathNumbers(root));}}
Solution
This problem follows the Binary Tree Path Sum pattern. We can follow the same ...