Skip to main content

Symmetric Tree


Symmetric Tree: Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

Example 1:

image

Input: root = [1,2,2,3,4,4,3]
Output: true

Example 2:

image

Input: root = [1,2,2,null,3,null,3]
Output: false

Constraints:
  • The number of nodes in the tree is in the range [1, 1000].
  • -100 <= Node.val <= 100

Try this Problem on your own or check similar problems:

Solution:
/**
* 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) {
if(root.left == null || root.right == null) return root.left == root.right;
return helper(root.left, root.right);
}
private boolean helper(TreeNode left, TreeNode right){
if(left == null || right == null) return left == right;
if(left.val != right.val) return false;
return helper(left.left, right.right) && helper(left.right, right.left);
}
}

Time/Space Complexity:
  • Time Complexity: O(n)
  • Space Complexity: O(h)

Explanation:

We traverse all nodes leading to linear time complexity proportional to the number of nodes in the tree and since we have recursion stack up to the height of the tree, we have space complexity of O(h) where h is the height of the tree. We first check if both children of root are defined, if not we check if both are null to have a symmetry. We utilize the helper function that checks recursively if the subtrees are symmetric, by first checking if both subtrees are defined and have equal values. Then we check if the left child of left subtree is symmetric to the right child of right subtree and if right child of left subtree is symmetric to left child of right subtree. If both requirements are fulfilled, we know the subtrees are symmetric and we return true. If we want to check if tree is symmetric iteratively, we can use stack and mimic the recursion (getting the same time and space complexity):

public boolean isSymmetric(TreeNode root) {
if(root.left == null || root.right == null) return root.left == root.right;

Stack<TreeNode> s = new Stack<>();
s.push(root.right);
s.push(root.left);


while(s.size() > 1){
TreeNode left = s.pop();
TreeNode right = s.pop();

if((left == null || right == null) && left != right) return false;
if(left == null) continue;
if(left.val != right.val) return false;

s.push(right.left);
s.push(left.right);
s.push(right.right);
s.push(left.left);
}

return s.empty();
}

Note that we put the nodes in reverse order of recursion calls in the stack since stack is LIFO (last in first out) data structure.