Monday, May 16, 2022

Easy_Question7 : Symmetric Tree

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


  • The number of nodes in the tree is in the range [1, 1000].
  • -100 <= Node.val <= 100
public boolean isSymmetric(TreeNode root) { if (root == null) return true; return isSymmetric(root.left, root.right); } public boolean isSymmetric(TreeNode l, TreeNode r) { if (l == null && r == null) { return true; } else if (r == null || l == null) { return false; } if (l.val != r.val) return false; if (!isSymmetric(l.left, r.right)) return false; if (!isSymmetric(l.right, r.left)) return false; return true; }

Follow on LinkedIn

You may also like

Kubernetes AWS Java Coding Question
Microservices Core Java Spring Boot
Spring Framework Kafka Miscellaneous