0
0
DSA Javascriptprogramming~20 mins

Check if Two Trees are Symmetric in DSA Javascript - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Symmetry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of symmetric tree check function
What is the output of the following JavaScript code that checks if two binary trees are symmetric?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isSymmetric(t1, t2) {
  if (!t1 && !t2) return true;
  if (!t1 || !t2) return false;
  if (t1.val !== t2.val) return false;
  return isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);
}

const treeA = new TreeNode(1, new TreeNode(2), new TreeNode(3));
const treeB = new TreeNode(1, new TreeNode(3), new TreeNode(2));

console.log(isSymmetric(treeA, treeB));
Atrue
Bfalse
CTypeError
DSyntaxError
Attempts:
2 left
💡 Hint
Check if the left subtree of one tree matches the right subtree of the other and vice versa.
Predict Output
intermediate
2:00remaining
Output when one tree is null
What is the output of the code below when checking symmetry between a non-empty tree and a null tree?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isSymmetric(t1, t2) {
  if (!t1 && !t2) return true;
  if (!t1 || !t2) return false;
  if (t1.val !== t2.val) return false;
  return isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);
}

const treeA = new TreeNode(1);
const treeB = null;

console.log(isSymmetric(treeA, treeB));
Afalse
Btrue
CReferenceError
DTypeError
Attempts:
2 left
💡 Hint
If one tree is null and the other is not, they cannot be symmetric.
🔧 Debug
advanced
2:00remaining
Identify the output of a symmetric tree check with nested nodes
What is the output of this code that checks if two trees with nested nodes are symmetric?
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isSymmetric(t1, t2) {
  if (!t1 && !t2) return true;
  if (!t1 || !t2) return false;
  if (t1.val !== t2.val) return false;
  return isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);
}

const treeA = new TreeNode(1, new TreeNode(2, new TreeNode(3), null), new TreeNode(4));
const treeB = new TreeNode(1, new TreeNode(4), new TreeNode(2, null, new TreeNode(3)));

console.log(isSymmetric(treeA, treeB));
Atrue
Bfalse
CRangeError
DSyntaxError
Attempts:
2 left
💡 Hint
Check if the left subtree of one tree matches the right subtree of the other recursively.
🧠 Conceptual
advanced
1:30remaining
Understanding the base cases in symmetric tree check
In the function to check if two trees are symmetric, why do we return true when both nodes are null and false when only one is null?
ABecause returning false for two null nodes would cause infinite recursion.
BBecause null nodes are ignored in symmetry checks.
CBecause the function only compares values, not structure.
DBecause two null nodes mean both subtrees ended symmetrically, but one null means asymmetry.
Attempts:
2 left
💡 Hint
Think about what it means for two subtrees to be mirrors when they are empty or partially empty.
🚀 Application
expert
2:30remaining
Number of symmetric subtree pairs in two trees
Given two binary trees, how many pairs of nodes (one from each tree) form symmetric subtrees? Consider the code below and determine the output.
DSA Javascript
class TreeNode {
  constructor(val, left = null, right = null) {
    this.val = val;
    this.left = left;
    this.right = right;
  }
}

function isSymmetric(t1, t2) {
  if (!t1 && !t2) return true;
  if (!t1 || !t2) return false;
  if (t1.val !== t2.val) return false;
  return isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);
}

function countSymmetricPairs(t1, t2) {
  if (!t1 && !t2) return 0;
  if (!t1 || !t2) return 0;
  let count = 0;
  if (isSymmetric(t1, t2)) count = 1;
  return count + countSymmetricPairs(t1.left, t2.right) + countSymmetricPairs(t1.right, t2.left);
}

const treeA = new TreeNode(1, new TreeNode(2, new TreeNode(3), null), new TreeNode(4));
const treeB = new TreeNode(1, new TreeNode(4), new TreeNode(2, null, new TreeNode(3)));

console.log(countSymmetricPairs(treeA, treeB));
A1
B2
C4
D3
Attempts:
2 left
💡 Hint
Count each pair of nodes that form symmetric subtrees including leaf nodes.