Found 7197 Articles for C++

Distribute Coins in Binary Tree in C++

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

228 Views

Suppose we have the root of a binary tree with N nodes, here each node in the tree has node.val number of coins, and there are N coins in total. In one move, we can choose two adjacent nodes and move only one coin from one node to the another node. (The move may be from parent to child node, or from child to parent node.). We have to find the number of moves required to make every node have exactly one coin.So if the tree is like −Then the output will be 3. From the left child, send 2 ... Read More

Pancake Sorting in C++

Arnab Chakraborty
Updated on 02-May-2020 10:18:14

645 Views

Suppose we have an array A, we will perform the Pancake sort technique on A. Here the main constraint is that we can use only one operation called rev(arr, i). This will reverse the elements of arr from 0 to ith position. This idea is like the selection sort. We repeatedly place the max element at end reduce the size of the array. So if the input is like [54, 85, 52, 25, 98, 75, 25, 11, 68], then the result will be [11, 25, 25, 52, 54, 68, 75, 85, 98]To solve this, we will follow these steps −size ... Read More

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

420 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

273 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

675 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

637 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

Advertisements