Found 33676 Articles for Programming

Program to perform level order traversal of binary tree in C++

Arnab Chakraborty
Updated on 10-Oct-2020 11:10:22

261 Views

Suppose we have a binary tree. We have to traverse this tree using level order traversal fashion. So if the tree is likeThe traversal sequence will be like: [1, 2, 3, 5, 4]To solve this, we will follow these steps −define queue que to store nodesinsert root into the que.while que is not empty, doitem := item present at front position of queueprint the value of itemif left of the item is not null, then insert left of item into queif right of the item is not null, then insert right of item into quedelete front element from queLet us ... Read More

Program to convert level order binary tree traversal to linked list in Python

Arnab Chakraborty
Updated on 10-Oct-2020 11:02:09

417 Views

Suppose we have a binary search tree, we have to convert it to a singly linked list using levelorder traversal.So, if the input is likethen the output will be [5, 4, 10, 2, 7, 15, ]To solve this, we will follow these steps −head := a new linked list nodecurrNode := headq := a list with value rootwhile q is not empty, docurr := delete first element from qif curr is not null, thennext of currNode := a new linked list node with value of currcurrNode := next of currNodeinsert left of curr at the end of qinsert right curr ... Read More

Program to traverse binary tree level wise in alternating way in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:54:23

142 Views

Suppose we have binary tree, we have to show the values of each level by alternating from going left-to-right and right-to-left.So, if the input is likethen the output will be [5, -10, 4, -2, -7, 15]To solve this, we will follow these steps −if root is null, thenreturn a new lists1 := a list initially insert roots2 := a new listres := a new listwhile s1 is not empty or s2 is not empty, dowhile s1 is not empty, donode := delete last element from s1if left of node is not null, theninsert left of node at the end of ... Read More

Program to find length of longest balanced subsequence in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:47:50

371 Views

Suppose we have a string s containing brackets parenthesis "(" and ")", we have to find the length of the longest subsequence of balanced brackets.So, if the input is like s = "())(()(", then the output will be 4, as we can take the subsequence like "()()"To solve this, we will follow these steps −res := 0n := size of sclose := 0for i in range n - 1 to 0, decrease by 1, doif s[i] is same as ")", thenclose := close + 1otherwise, if close > 0, thenclose := close - 1res := res + 2return resLet us ... Read More

Program to find the left side view of a binary tree in C++

Arnab Chakraborty
Updated on 10-Oct-2020 10:43:56

110 Views

Suppose we have a binary tree, if we see the tree from left side, then we can see some elements of it. we have to display those elements. So if the tree is like −The output will be [1, 2, 5]To solve this, we will follow these steps −Define an array retDefine a function dfs(), this will take node, c initialize it with 1, if node is null, then −returnif c > lvl, then −lvl := cinsert value of node into retdfs(left of node, c + 1)dfs(right of node, c + 1)From the main method, do the following −lvl := ... Read More

Program to find leftmost deepest node of a tree in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:34:00

379 Views

Suppose we have a binary tree; we have to find the value of the deepest node. If there are more than one deepest node, then return the leftmost deepest node.So, if the input is likethen the output will be 4 as 4 and 7 are deepest but 4 is left most.To solve this, we will follow these steps −queue := a queue with one node root.left_max := value of rootwhile size of queue > 0, dolevel_size := size of queuefor i in range 0 to level_size, dotemp := first element from queue and delete itif i is same as 0, ... Read More

Program to check whether all leaves are at same level or not in Python

Arnab Chakraborty
Updated on 10-Oct-2020 10:28:22

165 Views

Suppose we have a binary tree; we have to check whether all leaves are at the same level or not.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function dfs() . This will take root, dif root is not null, thenif left of root is null and right of root is null, theninsert d at the end of depthotherwise, dfs(left of root, d + 1)dfs(right of root, d + 1)From the main method, do the following −depth := a new listdfs(root, 0)return true when depth has only one valueLet ... Read More

How to create a matrix using vector of string values in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:40:50

2K+ Views

You might have heard that matrix can contain only numerical values but it is also possible to create a matrix with string values, and of course calculations using these types of matrices would not be possible. To create a matrix using string values, we can first create a vector of strings then define its dimension with dim function and that will convert the vector into matrix of string values.Example Live DemoM1

How to find the position of one or more values in a vector into another vector that contains same values in R?

Nizamuddin Siddiqui
Updated on 09-Oct-2020 15:33:10

725 Views

Finding the position of one of more values that are common in two vectors can be easily done with the help of match function. The match function will match the values in first and second vector then return the index or position of these common values in second vector.Example Live Demoset.seed(145) x1

Program to check whether we can fill square where each row and column will hold distinct elements in Python

Arnab Chakraborty
Updated on 09-Oct-2020 15:39:48

91 Views

Suppose we have one n × n matrix containing values from 0 to n. Here 0 represents an unfilled square, we have to check whether we can fill empty squares such that in each row and each column every number from 1 to n appears exactly once.So, if the input is like002201123then the output will be True, as we can set the matrix to312231123To solve this, we will follow these steps −Define a function find_empty_cell() . This will take matrix, nfor i in range 0 to n, dofor j in range 0 to n, doif matrix[i, j] is same as ... Read More

Advertisements