Found 26504 Articles for Server Side Programming

Check Completeness of a Binary Tree in C++

Arnab Chakraborty
Updated on 02-May-2020 10:17:15

229 Views

Suppose we have a binary tree. We have to check whether the tree is a complete binary tree or not. A complete binary tree of level n, has n-1 complete levels, and all nodes at level n, are filled from the left. So if the input tree is like −Then the output will be true, As this is complete binary tree.To solve this, we will follow these steps −If tree is empty, then return nullmake a queue q and insert root into itset flag := truewhile q has some elementssz := size of the queuewhile sz is not 0node := ... Read More

Flip Equivalent Binary Trees in C++

Arnab Chakraborty
Updated on 02-May-2020 10:11:36

310 Views

Suppose we have a binary tree. We have to flip the binary tree. The flip indicates: choose any node, and swap the left and right child subtrees. Now a binary tree X is flip equivalent to a binary tree Y if and only if we can make Y from X, after some number of flip operations. We have to write a method that determines whether two binary trees are flip equivalent. The trees are given by root nodes root1 and root2. So if the trees are −Then the output will be true, if we flip nodes with values 1, 3 ... Read More

Validate Stack Sequences in C++

Arnab Chakraborty
Updated on 02-May-2020 10:09:17

421 Views

Suppose we have two sequences pushed and popped with distinct values, we have to find true if and only if this could have been the result of a sequence of the push and pop operations on an initially empty stack. So if the input is push = [1, 2, 3, 4, 5], and pop = [4, 5, 3, 2, 1], then the output will be true. We can use push(1), push(2), push(3), push(4), pop() : 4, push(5), pop() : 5, pop() : 3, pop() : 2, pop() : 1To solve this, we will follow these steps −Create one method called ... Read More

Maximum Sum Circular Subarray in C++

Arnab Chakraborty
Updated on 02-May-2020 10:06:27

275 Views

Suppose we have a circular array C of integers represented by A, we have to find the maximum possible sum of a non-empty subarray of C. Also, a subarray may only include each element of the fixed buffer A at most once. If the array is like [1, -2, 3, -2], then the output will be 3. This is because subarray[3] has maximum sum 3.To solve this, we will follow these steps −n := size of vcreate arrays leftSum, leftSumMax, rightSum, rightSumMax all of size nleftSum[0] := v[0], leftSumMax[0] := maximum of 0 and v[0]for i in range 1 to ... Read More

Binary Tree Pruning in C++

Arnab Chakraborty
Updated on 02-May-2020 10:04:21

676 Views

Suppose we have the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. We have to find the same tree where every subtree not containing a 1 has been deleted. So if the tree is like −To solve this, we will follow these steps −Define a recursive method solve(), this will take the node. the method will be like −If node is null, then return nullleft of node := solve(left of node)right of node := solve(right of node)if left of node is null and right of node is also null ... Read More

Maximum Width of Binary Tree in C++

Arnab Chakraborty
Updated on 02-May-2020 09:59:28

200 Views

Suppose we have a binary tree, we have to define a function to get the maximum width of the given tree. Here the width of a tree is the maximum width among all levels. We will consider the binary tree has the same structure as a full binary tree, but some nodes are null. The width of one level is actually the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted for the length calculation). So if the tree is like −Then the maximum width ... Read More

Maximum Binary Tree in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:31:49

638 Views

Suppose we have an integer array. All elements in that array is unique. A maximum tree building on this array is defined as follow −The root will hold the maximum number in the array.The left subtree is the maximum tree constructed from left side of the subarray divided by the maximum number.The right subtree is the maximum tree constructed from right side of subarray divided by the maximum number.We have to construct the maximum binary tree. So if the input is like: [3, 2, 1, 6, 0, 5], then the output will be −To solve this, we will follow these ... Read More

Find Largest Value in Each Tree Row in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:27:57

192 Views

Suppose we have a binary tree, we have to find the largest elements of each level of that tree. So if the tree is like −Then the output will be [3, 5, 8]To solve this, we will follow these steps −Define an array called ansdefine a recursive function solve(), this will take tree node, and level, the level is initially 0. this method will act like −if node is null, then returnif level = size of ans, then insert node value into ans, otherwise ans[level] := max of ans[level] and node valuecall solve(left subtree of node, level + 1)call solve(right ... Read More

Find Bottom Left Tree Value in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:25:32

240 Views

Suppose we have a binary tree. We have to find the left most value of the last row of that tree. So if the tree is like −Then the output will be 7, as the last row is [7, 4], and left most element is 7.To solve this, we will follow these steps −initially define ans and lvl variable as 0define one method called solve(), this will take the tree node, and level, the level is initially 0. This will act as follows −if node is null, then returnif level > lvl, then ans := value of node and lvl ... Read More

Most Frequent Subtree Sum in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:24:15

320 Views

Suppose we have the root of a tree, we have to find the most frequent subtree sum. The subtree sum of a node is actually the sum of all the node values formed by the subtree rooted at that node (including the node itself). The most frequent subtree sum is actually If there is a tie, return all the values with the highest frequency in any order. So if the tree is like [5, 2, -5], then it will return [2]. This is because 2 happens twice, however -5 only occurs once.To solve this, we will follow these steps −Define ... Read More

Advertisements