0
0
DSA Javascriptprogramming~20 mins

Path Sum Root to Leaf in Binary Tree in DSA Javascript - 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
What is the output of the path sum check?
Given the binary tree and target sum, what is the output of the function call?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function hasPathSum(root, targetSum) {
  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)
    )
  ),
  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
Check if any root-to-leaf path sums exactly to 22.
Predict Output
intermediate
2:00remaining
What is the output when no path matches the sum?
What does the function return for the given tree and target sum 26?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function hasPathSum(root, targetSum) {
  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, 26));
ARangeError
Btrue
CReferenceError
Dfalse
Attempts:
2 left
💡 Hint
No root-to-leaf path sums to 26 here.
🔧 Debug
advanced
2:00remaining
What error does this code raise?
Identify the error when running this code snippet.
DSA Javascript
function hasPathSum(root, targetSum) {
  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 = { val: 1, left: null, right: null };
console.log(hasPathSum(tree, 1));
AReferenceError
BTypeError
CSyntaxError
DNo error, returns true
Attempts:
2 left
💡 Hint
Check for missing or mismatched parentheses.
Predict Output
advanced
2:00remaining
What is the output of this iterative path sum check?
What does the function return for the given tree and target sum 18?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function hasPathSumIterative(root, targetSum) {
  if (!root) return false;
  const stack = [{ node: root, currentSum: root.val }];
  while (stack.length) {
    const { node, currentSum } = stack.pop();
    if (!node.left && !node.right && currentSum === targetSum) return true;
    if (node.right) stack.push({ node: node.right, currentSum: currentSum + node.right.val });
    if (node.left) stack.push({ node: node.left, currentSum: currentSum + node.left.val });
  }
  return false;
}

const tree = new TreeNode(10,
  new TreeNode(5,
    new TreeNode(3),
    new TreeNode(2)
  ),
  new TreeNode(7)
);

console.log(hasPathSumIterative(tree, 18));
Atrue
Bfalse
CTypeError
DSyntaxError
Attempts:
2 left
💡 Hint
Check if any root-to-leaf path sums to 18 using stack.
🧠 Conceptual
expert
2:00remaining
How many root-to-leaf paths sum to 22?
Given the binary tree below, how many root-to-leaf paths have a sum equal to 22? Tree structure: 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1
A2
B1
C3
D4
Attempts:
2 left
💡 Hint
Trace all root-to-leaf paths and sum their values.