0
0
DSA Typescriptprogramming~20 mins

Check if Two Trees are Symmetric in DSA Typescript - 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 check on two trees
What is the output of the following TypeScript code that checks if two binary trees are symmetric?
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 isSymmetric(t1: TreeNode | null, t2: TreeNode | null): boolean {
  if (!t1 && !t2) return true;
  if (!t1 || !t2) return false;
  return t1.val === t2.val && isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);
}

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

console.log(isSymmetric(treeA, treeB));
ATypeError
Bfalse
CSyntaxError
Dtrue
Attempts:
2 left
💡 Hint
Check if the left subtree of one tree matches the right subtree of the other tree recursively.
🧠 Conceptual
intermediate
1:30remaining
Understanding symmetry in binary trees
Which of the following best describes when two binary trees are symmetric?
AThey have the same number of nodes regardless of structure.
BThey have the same values in the same positions.
COne tree is the mirror image of the other, with matching node values.
DThey have identical left subtrees but different right subtrees.
Attempts:
2 left
💡 Hint
Symmetry means one tree looks like the mirror reflection of the other.
🔧 Debug
advanced
2:00remaining
Identify the error in symmetric tree check
What error will this TypeScript code produce when checking if two trees are symmetric?
DSA Typescript
function isSymmetric(t1: TreeNode | null, t2: TreeNode | null): boolean {
  if (!t1 && !t2) return true;
  if (!t1 || !t2) return false;
  return t1.val === t2.val && isSymmetric(t1.left, t2.left) && isSymmetric(t1.right, t2.right);
}
AReturns false for symmetric trees due to wrong recursive calls
BSyntaxError due to missing return statement
CNo error, returns correct result
DTypeError because of null property access
Attempts:
2 left
💡 Hint
Check if the recursive calls compare the correct subtrees for symmetry.
Predict Output
advanced
1:00remaining
Output of symmetric check with null trees
What is the output of this TypeScript code when both trees are null?
DSA Typescript
function isSymmetric(t1: TreeNode | null, t2: TreeNode | null): boolean {
  if (!t1 && !t2) return true;
  if (!t1 || !t2) return false;
  return t1.val === t2.val && isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left);
}

console.log(isSymmetric(null, null));
Afalse
BTypeError
CSyntaxError
Dtrue
Attempts:
2 left
💡 Hint
Two empty trees are symmetric by definition.
🚀 Application
expert
3:00remaining
Number of symmetric subtree pairs in two trees
Given two binary trees, how many pairs of subtrees (one from each tree) are symmetric according to the isSymmetric function below? Consider these trees: Tree1: 1 / \ 2 3 / \ 4 5 Tree2: 1 / \ 3 2 / \ 5 4 Use the isSymmetric function: function isSymmetric(t1: TreeNode | null, t2: TreeNode | null): boolean { if (!t1 && !t2) return true; if (!t1 || !t2) return false; return t1.val === t2.val && isSymmetric(t1.left, t2.right) && isSymmetric(t1.right, t2.left); }
A5
B4
C3
D6
Attempts:
2 left
💡 Hint
Count all subtree pairs including the root and leaf nodes that are mirror images.