
- 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 inorder sequence of a tree is palindrome or not in Python
Suppose we have a binary tree where each node contains a digit from 0-9, we have to check whether its in-order traversal is palindrome or not.
So, if the input is like
then the output will be True, as its inorder traversal is [2,6,10,6,2].
To solve this, we will follow these steps −
- if root is null, then
- return True
- stack := a new stack
- curr := root
- inorder := a new list
- while stack is not empty or curr is not null, do
- while curr is not null, do
- push curr into stack
- curr := left of curr
- node := popped element from stack
- insert value of node at the end of inorder
- curr := right of node
- while curr is not null, do
- return true when inorder is same as inorder in reverse order, otherwise false.
Let us see the following implementation to get better understanding −
Example
class TreeNode: def __init__(self, data, left = None, right = None): self.val = data self.left = left self.right = right class Solution: def solve(self, root): if not root: return True stack = [] curr = root inorder = [] while stack or curr: while curr: stack.append(curr) curr = curr.left node = stack.pop() inorder.append(node.val) curr = node.right return inorder == inorder[::-1] ob = Solution() root = TreeNode(6) root.left = TreeNode(2) root.right = TreeNode(6) root.right.left = TreeNode(10) root.right.right = TreeNode(2) print(ob.solve(root))
Input
root = TreeNode(6) root.left = TreeNode(2) root.right = TreeNode(6) root.right.left = TreeNode(10) root.right.right = TreeNode(2)
Output
True
- Related Articles
- Program to check whether given tree is symmetric tree or not in Python
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- C++ Program to Check Whether a Number is Palindrome 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
- How to Check Whether a String is Palindrome or Not using Python?
- Program to check a string is palindrome or not in Python
- Python program to check if a string is palindrome or not
- Program to check whether a tree is height balanced or not in C++
- Python program to check whether the string is Symmetrical or Palindrome
- Write a Golang program to check whether a given number is a palindrome or not
- Check if an array represents Inorder of Binary Search tree or not in Python
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- 8085 program to check whether the given 16 bit number is palindrome or not

Advertisements