

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if an array represents Inorder of Binary Search tree or not in Python
Suppose we have an array of numbers called nums. We have to check whether the array is holding elements of a binary search tree in the sequence of its inorder traversal or not.
So, if the input is like nums = [5, 8, 15, 18, 20, 26, 39], then the output will be True as this is inorder traversal of
To solve this, we will follow these steps −
- size := size of nums
- if size either 0 or 1, then
- return True
- for i in range 1 to size - 1, do
- if nums[i - 1] > nums[i], then
- return False
- if nums[i - 1] > nums[i], then
- return True
Let us see the following implementation to get better understanding −
Example
def solve(nums): size = len(nums) if size == 0 or size == 1: return True for i in range(1, size): if nums[i - 1] > nums[i]: return False return True nums = [5, 8, 15, 18, 20, 26, 39] print(solve(nums))
Input
[5, 8, 15, 18, 20, 26, 39]
Output
True
- Related Questions & Answers
- Check if an encoding represents a unique binary string in Python
- C++ program to Check if a Given Binary Tree is an AVL Tree or Not
- Binary Tree Inorder Traversal in Python
- Python Program to Build Binary Tree if Inorder or Postorder Traversal as Input
- Program to check whether inorder sequence of a tree is palindrome or not in Python
- Check if a binary tree is sorted levelwise or not in C++
- Program to find Inorder Successor of a binary search tree in C++
- Program to perform an Inorder Traversal of a binary tree in Python
- Check if a binary tree is sorted level-wise or not in C++
- Check if a given array can represent Preorder Traversal of Binary Search Tree in C++
- Find if given vertical level of binary tree is sorted or not in Python
- A program to check if a binary tree is BST or not in C ?
- Check if an array is synchronized or not in C#
- Check if a given graph is tree or not
- Python - Check if a given string is binary string or not
Advertisements