101 Symmetric tree

Dhanaraj S
Apr 13, 2022


Check the problem description here


Recursive solution

Recursively check if the mirror nodes are same.

class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
if root:
return self.helper(root.left,root.right)
return False
def helper(self,n1,n2):
if n1==None or n2==None :
return n1==n2
if n1.val != n2.val:
return False
return (self.helper(n1.left,n2.right) and self.helper(n1.right,n2.left))

Time :O(N)

Space :O(h) h = height of tree



Dhanaraj S

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.