Found 33676 Articles for Programming

All Nodes Distance K in Binary Tree in C++

Arnab Chakraborty
Updated on 17-Nov-2020 11:14:04

268 Views

Suppose we have a binary tree, a target node, and a one value K. We have to find a list of the values of all nodes that have a distance K from the target node.So, if the input is like root = [3, 5, 1, 6, 2, 0, 8, null, null, 7, 4], target = 5, K = 2, then the output will be [7, 4, 1], this is because the nodes that are a distance 2 from the target node have values 7, 4, and 1.To solve this, we will follow these steps −Define a function dfs(), this will ... Read More

Max Increase to Keep City Skyline in Python

Arnab Chakraborty
Updated on 17-Nov-2020 11:09:19

271 Views

Suppose we have a 2-dimensional array called grid, where each value of grid[i][j] represents the height of a building located there. We can increase the height of any number of buildings, by any amount. Height 0 is considered to be a building as well. At the end, the "skyline" when viewed from all four directions of the grid, must be the same as the skyline of the original grid. Because a city's skyline is the outer contour of the rectangles formed by all the buildings when viewed from a distance. So we have to find the maximum total sum that ... Read More

Cheapest Flights Within K Stops in C++

Arnab Chakraborty
Updated on 17-Nov-2020 11:05:59

679 Views

Suppose we have n cities connected by m flights. Each flight starts from u and arrives at v with a price w. If we have all the cities and flights, together with starting city src and the destination dst, here our task is to find the cheapest price from src to dst with up to k stops. If there is no such route, then return -1.So, if the input is like n = 3, edges = [[0, 1, 100], [1, 2, 100], [0, 2, 500]], src = 0, dst = 2, k = 1, then the output will be 200To ... Read More

Redundant Connection in C++

Arnab Chakraborty
Updated on 17-Nov-2020 11:03:03

411 Views

Suppose we have one unrooted tree; this is one undirected graph with no cycles. The given input is a graph that started as a tree with N nodes (values of the nodes are distinct values ranging from 1 through N), with one additional edge added. The added edge has two different vertices chosen from 1 to N, and was not an edge that already existed.The final graph is given as a 2D-array of edges. Each element of edges is a pair [u, v] where u < v, that represents an undirected edge connecting nodes u and v.We have to find ... Read More

Add One Row to Tree in C++

Nishu Kumari
Updated on 30-Jul-2025 19:18:18

247 Views

We are given the root of a binary tree and two integers, val and depth. The root of the binary tree is at depth 1. Our task is to add a new row of nodes with value val at the given depth. If the depth is 1, we have to create a new root node with value val and make the original tree its left child. If the depth is greater than 1, we go to all nodes at depth depth - 1, add new left and right child nodes with value val, and connect the original left and right ... Read More

Restore IP Addresses in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:55:26

458 Views

Suppose we have a string containing only digits, we have to restore it by returning all possible valid IP address combinations. We know that a valid IP address consists of exactly four integers (each integer is in range 0 to 255) separated by single points.So, if the input is like "25525511135", then the output will be ["255.255.11.135", "255.255.111.35"]To solve this, we will follow these steps −Define a function convertToNum(), this will take s, start, end, num := 0for initialize i := start, when i 255, then −return 10000return numDefine a function addDots(), this will take positions, res := blank ... Read More

Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:49:21

227 Views

Suppose we have a binary tree where each path going from the root to any leaf form a valid sequence, we have to check whether a given string is a valid sequence in such binary tree or not.We will get the given string from the concatenation of an array of integers arr and the concatenation of all values of the nodes along a path results in a sequenceSuppose we have a binary tree like.So, if arr = [0, 1, 0, 1], then the output will be True, as the path 0 -> 1 -> 0 -> 1 is a valid ... Read More

First Unique Number in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:45:29

602 Views

Suppose we have a queue of integers, we need to retrieve the first unique integer in that queue. We have to implement the class called FirstUnique: It will be initialized by the numbers in the queue. Define one function showFirstUnique(), this will return the value of the first unique integer of the queue and returns -1 if there is no such integer. Another method is add(value) this will insert value to the queue.So, if the input is likeInitialize with [2, 3, 4] then call the functions as follows −showFirstUnique()add(5)showFirstUnique()add(2)showFirstUnique()add(3)showFirstUnique(), then the output will be 2, 2, 3, -1 respectively.To solve ... Read More

Sentence Similarity II in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:43:12

564 Views

Suppose we have Given two arrays words1, words2 these are considered as sentences, and a list of similar word pairs, we have to check whether two sentences are similar or not. So if the input is like words1 = ["great", "acting", "skills"] and words2 = ["fine", "drama", "talent"] these two are similar, if the similar word pairs are like = [["great", "good"], ["fine", "good"], ["acting", "drama"], ["skills", "talent"]].The similarity relation is transitive. For example, if "great" and "good" are similar, and "fine" and "good" are similar, then "great" and "fine" are also similar. And the similarity is also symmetric. So, ... Read More

Insert into a Sorted Circular Linked List in C++

Arnab Chakraborty
Updated on 17-Nov-2020 10:40:07

393 Views

Suppose we have a node from a Circular Linked List which is sorted in increasing order, we have to define a function to insert a value insertVal into the list such that it remains a sorted circular list.The node can be a reference to any single node in the list, and may not be necessarily the first value of the circular list. If there are multiple suitable places for insertion, we can choose any place to insert the new value. If the list is empty, then we have to create a new single circular list and return the reference to ... Read More

Advertisements