Intermediate Level
Implement the symmetricBinaryTreeCheck method that checks whether a binary tree is a mirror of itself.
You are given binary tree values in level-order array form, where 0 represents a missing node. Return true if the tree is symmetric around its center; otherwise return false.
A tree is symmetric when the left subtree and right subtree are mirror images of each other.
Compare the tree in mirrored pairs.
Two nodes are mirrors if their values are equal, the left child of the first node mirrors the right child of the second node, and the right child of the first node mirrors the left child of the second node.
Use a recursive helper or a queue of node pairs to perform these comparisons.
Pseudocode:
function symmetricBinaryTreeCheck(tree, size):
if size == 0:
return true
function isMirror(leftIndex, rightIndex):
if both indexes are missing:
return true
if only one index is missing:
return false
if tree[leftIndex] != tree[rightIndex]:
return false
return isMirror(left child of leftIndex, right child of rightIndex)
and isMirror(right child of leftIndex, left child of rightIndex)
return isMirror(left child of root, right child of root)