LeetCode 101. Symmetric Tree
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
LeetCode 101. Symmetric Tree
Problem Description
Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Example 1:
Output: True
Example 2:
Output: False
Approach
Consider this “mirror of itself”. Imagine you are given two trees and asked to check whether they are mirrors of each other. We start with the roots of each tree. If the roots are both null, then we are done, they are trivially mirrors of each other. If only one of the roots is null, then return false. Otherwise, we check if the values of the roots are the same. Since they are mirrors of each other, we check if the left subtree of the root of the first tree is the mirror of the right subtree of the root of the second tree and also the right subtree of the root of the first tree is the mirror of the left subtree of the root of the second tree.
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
boolean isMirror(TreeNode root1, TreeNode root2){
if(root1 == null && root2 == null) return true;
if((root1 == null && root2 != null ) || (root1 != null && root2 == null)) return false;
return root1.val == root2.val && isMirror(root1.left, root2.right)
&& isMirror(root1.right, root2.left);
}
public boolean isSymmetric(TreeNode root) {
return isMirror(root, root);
}
}

