101. Symmetric Tree
EasySolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks us to determine if a binary tree is a mirror image of itself. This means that its left and right subtrees must be mirror images of each other. For example, if we fold the tree along a vertical line passing through the root, the left side should perfectly overlap with the right side.
Solution
Symmetric Tree in Python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
from typing import Optional
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
# A tree is symmetric if the root is null.
if not root:
return True
# Helper function to check if two trees are mirrors of each other.
def isMirror(t1: Optional[TreeNode], t2: Optional[TreeNode]) -> bool:
# If both trees are null, they are mirrors.
if not t1 and not t2:
return True
# If one of the trees is null, they are not mirrors.
if not t1 or not t2:
return False
# For them to be mirrors, their root values must be the same,
# and the left subtree of t1 must be a mirror of the right subtree of t2,
# and the right subtree of t1 must be a mirror of the left subtree of t2.
return (t1.val == t2.val) and \
isMirror(t1.left, t2.right) and \
isMirror(t1.right, t2.left)
# Check if the root's left and right subtrees are mirrors.
return isMirror(root.left, root.right)
Symmetric Tree in Java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
// A tree is symmetric if the root is null.
if (root == null) {
return true;
}
// Otherwise, its left and right subtrees must be mirror images of each other.
return isMirror(root.left, root.right);
}
// Helper function to check if two trees are mirrors of each other.
private boolean isMirror(TreeNode t1, TreeNode t2) {
// If both trees are null, they are mirrors.
if (t1 == null && t2 == null) {
return true;
}
// If one of the trees is null, they are not mirrors.
if (t1 == null || t2 == null) {
return false;
}
// For them to be mirrors, their root values must be the same,
// and the left subtree of t1 must be a mirror of the right subtree of t2,
// and the right subtree of t1 must be a mirror of the left subtree of t2.
return (t1.val == t2.val) &&
isMirror(t1.right, t2.left) &&
isMirror(t1.left, t2.right);
}
}
Symmetric Tree in C++
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
// A tree is symmetric if the root is null.
if (!root) {
return true;
}
// Otherwise, its left and right subtrees must be mirror images of each other.
return isMirror(root->left, root->right);
}
private:
// Helper function to check if two trees are mirrors of each other.
bool isMirror(TreeNode* t1, TreeNode* t2) {
// If both trees are null, they are mirrors.
if (!t1 && !t2) {
return true;
}
// If one of the trees is null, they are not mirrors.
if (!t1 || !t2) {
return false;
}
// For them to be mirrors, their root values must be the same,
// and the left subtree of t1 must be a mirror of the right subtree of t2,
// and the right subtree of t1 must be a mirror of the left subtree of t2.
return (t1->val == t2->val) &&
isMirror(t1->right, t2->left) &&
isMirror(t1->left, t2->right);
}
};
Symmetric Tree in TypeScript
/**
* Definition for a binary tree node.
* 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(root: TreeNode | null): boolean {
// A tree is symmetric if the root is null.
if (!root) {
return true;
}
// Helper function to check if two trees are mirrors of each other.
function isMirror(t1: TreeNode | null, t2: TreeNode | null): boolean {
// If both trees are null, they are mirrors.
if (!t1 && !t2) {
return true;
}
// If one of the trees is null, they are not mirrors.
if (!t1 || !t2) {
return false;
}
// For them to be mirrors, their root values must be the same,
// and the left subtree of t1 must be a mirror of the right subtree of t2,
// and the right subtree of t1 must be a mirror of the left subtree of t2.
return (t1.val === t2.val) &&
isMirror(t1.right, t2.left) &&
isMirror(t1.left, t2.right);
}
// Check if the root's left and right subtrees are mirrors.
return isMirror(root.left, root.right);
};
More Easy LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.