This article is another part of a series on leet code problems solutions, and it provides a solution to the leet code problem number 101. This is a Leet code problem named Symmetric Tree. We will solve it using python and it is the best space and time-optimized solution.
Question of Symmetric Tree
Given the root
of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).
Each node in a binary tree has a left subtree and a right subtree. An empty subtree, a single node, or another binary tree can all be subtrees. If the left subtree of the root node mirrors the right subtree, a binary tree is said to be symmetric. The binary tree in the first example is symmetric.
Example
Example 1
Input: root = [1,2,2,3,4,4,3]
Output: true
Example 2
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
Approach to solve Symmetric Tree
To solve this problem we will recursively call the left and right subtree and check whether they are mirror images of each other or not. If they are mirror images then we recursively call the function till we find none otherwise we return false.
Algorithm
This method performs the following steps for a given input binary search tree:
- Create an inner function to compare sides. We pass the left and the right node at a time to compare.
- Check base case if both left and right nodes are none then return true.
- If the left and right nodes exist and both nodes’ values are equal, do step 4.
- Call the compare side function twice and return and condition for them. The two parameters to call are, first, with left child of left node and right child of a right node, second left child of right node and right child of the left node.
- If any of the above conditions are false return False.
- end if
- End inner function
Python Code for Symmetric Tree
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
#inner function
def compareSides( left, right):
#base case
if left is None and right is None:
return True
#if left and right both value exists and equal
elif left and right and (left.val == right.val) :
# recursively call compareSides function for left and right side
return compareSides(left.left, right.right) and compareSides(left.right, right.left)
# otherwise return false
else:
return False
return compareSides(root.left, root.right)
Complexity Analysis
Time Complexity: O(n)
n is the number of nodes in the binary tree.
So here is the end of the post. If you find any issues or any questions then write a comment. Share with your friends.
Related Posts:
- Find Pivot Index (Leet Code-724)
- Spiral Matrix Problem Solution
- Isomorphic Strings
- Two Sum IV - Input is a BST (Leet Code-653)
- Search in a Binary Search Tree (Leet Code-700)
- Invert Binary Tree (Leet Code-266)
- Maximum Depth of Binary Tree (Leet Code-104)
- Binary Tree Postorder Traversal (Leet Code-145)
- Binary Tree Inorder Traversal (Leet Code - 94)
- Binary Tree Pre-Order Traversal
- Binary Tree Level Order Traversal (Leet Code-102)
- Valid Parentheses (Leet Code-20)
- Remove Duplicates from Sorted List (Leet Code-83)
- Reverse Linked List (Leet code -206)
- Remove Linked List Elements (Leet Code-203)
- Merge Two Sorted Lists (Leet code-21)
- Ransom Note (Leet code-383)
- First Unique Character in a String (Leet code-387)
- Valid Sudoku (Leet Code-36)
- Search a 2D Matrix (Leet Code-74)
- Linked List Cycle (Leet Code-141)
- Pascal's Triangle (Leet Code - 118)
- Reshape the Matrix (Leet Code-566)
- Best Time to Buy and Sell Stock (Leet code-121)
- The Intersection of Two Arrays II (Leet Code)
- Binary Search - Leet Code
- Two Sum - Leet Code