
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to check whether given tree is symmetric tree or not in Python
Suppose we have one binary tree. We have to check whether the tree is symmetric tree or not. A tree will be said to be symmetric if it is same when we take the mirror image of it. From these two trees, the first one is symmetric, but second one is not.
To solve this, we will follow these steps.
We will call following steps recursively. The function will be solve(root, root)
if the node1 and node2 are empty, then return true
if either node1 or node2 is empty, then return false
return true when node1.val = node2.val and solve(node1.left, node2.right) and solve(node1.right, node2.left)
Let us see the following implementation to get better understanding −
Example
class TreeNode: def __init__(self, data, left = None, right = None): self.data = data self.left = left self.right = right class Solution(object): def isSymmetric(self, root): return self.solve(root,root) def solve(self,node1,node2): if not node1 and not node2: return True if not node1 or not node2: return False return node1.data == node2.data and self.solve(node1.left,node2.right) and self.solve(node1.right,node2.left) root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(2) root.left.left = TreeNode(3) root.left.right = TreeNode(4) root.right.left = TreeNode(4) root.right.right = TreeNode(3) ob1 = Solution() print(ob1.isSymmetric(root))
Input
root = TreeNode(1) root.left = TreeNode(2) root.right = TreeNode(2) root.left.left = TreeNode(3) root.left.right = TreeNode(4) root.right.left = TreeNode(4) root.right.right = TreeNode(3)
Output
True
- Related Articles
- How to check whether the tree is symmetric or not using iterative in C#?
- How to check whether the tree is symmetric or not using recursion in C#?
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- Program to check whether a binary tree is complete or not in Python
- Program to check whether a binary tree is BST or not in Python
- Program to check whether one tree is subtree of other or not in Python
- Program to check whether inorder sequence of a tree is palindrome or not in Python
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- Program to check whether a tree is height balanced or not in C++
- C Program To Check whether Matrix is Skew Symmetric or not?
- Check if a given graph is tree or not
- Symmetric Tree in Python
- Program to check whether given list of blocks are symmetric over x = y line or not in python
- Program to check whether given graph is bipartite or not in Python

Advertisements