Check if a Binary Tree is Symmetric
viaGlassdoor
Problem: Write a function that takes the root node of a binary tree and returns true or false depending on whether the tree is symmetric (a mirror image of itself around its center).
Constraints: Tree can be empty (considered symmetric) or have any shape.
Example: A tree with root 1, children [2,2], grandchildren [3,4,4,3] is symmetric; [2,2] with grandchildren [null,3,null,3] is not.
Approach: Recursively compare the left and right subtrees as mirror images: isMirror(left, right) returns true if both are null, false if only one is null or values differ, else recurse on isMirror(left.left, right.right) && isMirror(left.right, right.left). O(n) time, O(h) space.
asked …