Suppose we have a list of numbers called nums, and another number k, we have to check whether the list can be split into lists where each list contains k values and the values are consecutively increasing.So, if the input is like nums = [4, 3, 2, 4, 5, 6], k = 3, then the output will be True, as we can split the list into [2, 3, 4] and [4, 5, 6]To solve this, we will follow these steps −Define one mapfor each key it in mincrease m[it] by 1ok := truewhile (size of m is not 0 and ... Read More
Suppose we have a sorted linked list node of size n, we have to create a binary search tree by Taking the value of the k = floor of (n / 2) the smallest setting it as the root. Then recursively constructing the left subtree using the linked list left of the kth node. And recursively constructing the right subtree using the linked list right of the kth node.So, if the input is like [2, 4, 5, 7, 10, 15], then the output will beTo solve this, we will follow these steps−Define a method solve(), this will take nodeif node ... Read More
Suppose we have a binary tree, the level of its root is 1, the level of its children is 2, and so on.We have to find the smallest level X such that the sum of all the values of nodes at level X is minimum. So if the tree is like −Output will be 2 as the sum is 4 – 10 = -6, which is minimum.To solve this, we will follow these steps −level := 1, sum := value of r, ansLevel := level, ansSum := sumdefine a queue q, insert given node r into qwhile q is not ... Read More
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
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
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
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
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
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
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