0
0
DSA Typescriptprogramming~20 mins

Path Sum Root to Leaf in Binary Tree in DSA Typescript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of path sum check for a simple tree
What is the output of the following TypeScript code that checks if a path sum exists from root to leaf?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
  if (!root) return false;
  if (!root.left && !root.right) return root.val === targetSum;
  return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
}

const tree = new TreeNode(5, new TreeNode(4, new TreeNode(11, new TreeNode(7), new TreeNode(2)), null), new TreeNode(8, new TreeNode(13), new TreeNode(4, null, new TreeNode(1))));
console.log(hasPathSum(tree, 22));
Atrue
Bfalse
CSyntaxError
DTypeError
Attempts:
2 left
💡 Hint
Think about whether there is a root-to-leaf path that sums to 22.
Predict Output
intermediate
2:00remaining
Output when no path matches the target sum
What will be printed when the following code runs?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
  if (!root) return false;
  if (!root.left && !root.right) return root.val === targetSum;
  return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
}

const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3));
console.log(hasPathSum(tree, 5));
Afalse
Btrue
Cundefined
DRuntimeError
Attempts:
2 left
💡 Hint
Check if any root-to-leaf path sums to 5.
🔧 Debug
advanced
2:00remaining
Identify the error in path sum function
What error will this code produce when run?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
  if (!root) return false;
  if (!root.left && !root.right) return root.val === targetSum;
  return hasPathSum(root.left, targetSum - root.val) && hasPathSum(root.right, targetSum - root.val);
}

const tree = new TreeNode(1, new TreeNode(2), new TreeNode(3));
console.log(hasPathSum(tree, 3));
AStackOverflowError
Bfalse
Ctrue
DTypeError
Attempts:
2 left
💡 Hint
Check the logical operator used in the recursive call.
Predict Output
advanced
2:00remaining
Output of path sum with negative values
What is the output of this code that includes negative values in the tree?
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
  if (!root) return false;
  if (!root.left && !root.right) return root.val === targetSum;
  return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
}

const tree = new TreeNode(-2, null, new TreeNode(-3));
console.log(hasPathSum(tree, -5));
ASyntaxError
Bfalse
Ctrue
DTypeError
Attempts:
2 left
💡 Hint
Consider the path -2 -> -3 and its sum.
🧠 Conceptual
expert
3:00remaining
Number of root-to-leaf paths with given sum
Given a binary tree, how many root-to-leaf paths sum to the target value 8 in the tree below? Tree structure: 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 Options:
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Find all root-to-leaf paths and sum their values.