Easy - Sum Root to Leaf Numbers
https://leetcode.com/problems/sum-root-to-leaf-numbers/description
Answer
class TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
constructor(val: number = 0, left: TreeNode | null = null, right: TreeNode | null = null) {
this.val = val;
this.left = left;
this.right = right;
}
}
function sumNumbers(root: TreeNode | null): number {
function dfs(node: TreeNode | null, currentSum: number): number {
if (!node) return 0;
currentSum = currentSum * 10 + node.val;
if (!node.left && !node.right) { // Check if leaf node
return currentSum;
}
return dfs(node.left, currentSum) + dfs(node.right, currentSum);
}
return dfs(root, 0);
}