Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Programming Articles
Page 221 of 2544
Check if leaf traversal of two Binary Trees is same in Python
Suppose we have two binary tree. We have to check whether leaf traversal of these two trees are same or not. As we know the leaf traversal is sequence of leaves traversed from left to right.So, if the input is likethen the output will be True as the left traversal sequence of both of the trees are same, that is [5, 7, 8].To solve this, we will follow these steps −s1 := a new list, s2 := another new listinsert r1 into s1 and r2 into s2while s1 and s2 are not empty, doif s1 is empty or s2 is ...
Read MoreCheck if linked list is sorted (Iterative and Recursive) in Python
Suppose we have a linked list we have to define two functions to check whether the linked list is sorted in non-increasing order or not. One of the method will work as iterative manner and another one in recursive manner.So, if the input is like L = [15, 13, 8, 6, 4, 2], then the output will be True.To solve this, we will follow these steps −Define a function solve_iter(). This will take headif head is null, thenreturn Truewhile next of head is not null, docurrent := headif value of current value of (next of head) is not 0 ...
Read MoreCount the nodes whose sum with X is a Fibonacci number in C++
Given a binary tree with weights of its nodes as numbers. The goal is to find the number of nodes that have weights such that the number is a Fibonacci number. Numbers in Fibonacci series are: 0, 1, 1, 2, 3, 5, 8, 13….nth number is the sum of (n−1)th and (n−2)th. If weight is 13 then it is a Fibonacci number so the node will be counted.For ExampleInputtemp =1. The tree which will be created after inputting the values is given below −OutputCount the nodes whose sum with X is a Fibonacci number are: 3Explanationwe are given with the ...
Read MoreCheck if lowercase and uppercase characters are in same order in Python
Suppose we have a string s with only lowercase or uppercase letters not numbers. We have to check whether both lowercase and uppercase letters follow the same order respectively or not. So, if a letter occurs more than once in lowercase then the occurrence of the same character in the uppercase will be same.So, if the input is like s = "piPpIePE", then the output will be True, as occurrences of lowercase letters and uppercase letters are same, and they are in the same order in lowercase and uppercase also.To solve this, we will follow these steps −lowercase := blank ...
Read MoreConstruct the full k-ary tree from its preorder traversal in C++
We are given an array arr[] containing the preorder traversal of the k-ary tree in sequence. The goal is to construct the same k-ary tree from it and print its postorder traversal. A full k−ary tree is the one in which the root node has 0 or k children i.e. at most k child.For ExampleInputint arr[] = {2, 5, 1, 3, 6, 7, 2, 1 }, int size = 8, int children = 2OutputThe full k−ary tree which will be constructed with the two children from preorder traversal is given below −Explanationwe are given with an array of integer values ...
Read MoreCheck if matrix A can be converted to B by changing parity of corner elements of any submatrix in Python
Suppose we have two N X M binary matrices A and B. In a single operation, we can select a sub-matrix (at least 2x2) and convert the parity of the corner elements (flip bits). Finally, we have to check whether the matrix A can be converted to B by performing any number of operations or not.So, if the input is like100101100then the output will be True as we can perform the operation on the top left square sub-matrix of size (2x2) on mat1 to get mat2.To solve this, we will follow these steps −row := row count of mat1column := ...
Read MoreCount pairs in a binary tree whose sum is equal to a given value x in C++
We are given an integer value and a variable x and the task is to construct the binary tree and find the pairs having sum equals to the given value x.For ExampleInputint x = 5, The tree which will be created after inputting the values is given below −OutputCount of pairs in a binary tree whose sum is equal to a given value x are: 2Explanationwe are given with an array of integer values that is used to form a binary tree and we will check whether there is a pair present in a binary tree whose sum equals to ...
Read MoreCheck if matrix can be converted to another matrix by transposing square sub-matrices in Python
Suppose we have two N X M called mat1 and mat2. In an operation, we can transpose any square sub-matrix in mat1. We have to check whether we can get mat2 from mat1 or not by performing given operation.So, if the input is like567123689562173689then the output will be True, because if we get transpose of top right sub-matrix of size 2x2 of mat1, we will get mat2.To solve this, we will follow these steps −row := row count of matricescolumn := column count of matricesfor i in range 0 to row - 1, dotemp1 := a new list, temp2 := ...
Read MoreConstruct Special Binary Tree from given Inorder traversal in C++
We are given an array arr[] containing the inorder traversal of a binary tree. The goal is to construct a special binary tree from that array. A special binary tree is the one which has root node’s weight greater than the weights of both its left and right children.For ExampleInputint arr[] = {10, 20, 28, 40, 32, 31, 30}OutputThe special binary tree which will be constructed with the given inorder traversal is given below −Explanationwe are given with an array of integer values or the inorder traversal of a tree. So, the special tree formed is 10, 20, 28, 40, ...
Read MoreCheck if Matrix remains unchanged after row reversals in Python
Suppose we have a square matrix. We have to check whether the matrix remains same after performing row reversal operations on each row, or not.So, if the input is like686282333then the output will be TrueTo solve this, we will follow these steps −n := row count of matrixfor i in range 0 to n - 1, doleft := 0, right := n - 1while left
Read More