Found 2587 Articles for Csharp

How to check whether a binary tree is a valid binary search tree using recursion in C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:42:20

429 Views

A tree is a binary search tree if it has all the left child lesser than the node elements and all the right child greater than the node elements. Initially we check whether the node has any value, if the node is null then we consider as valid binary search tree and return true. After checking the node null result, we call the recursive method isValidBST by passing the node, min value and max value. If the root value is lesser than min and the root value is greater than max we consider as not a binary search tree and ... Read More

How to invert a binary search tree using recursion in C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:39:48

2K+ Views

To invert a binary search tree, we call a method InvertABinarySearchTree which takes node as a parameter. If the node is null then return null, if the node is not null, we call the InvertABinarySearchTree recursively by passing the left and right child values. and assign the right child value to the left child and left child value to the right child. The final output will consist of the tree which will be its own mirror image.Examplepublic class TreesPgm{    public class Node{       public int Value;       public Node LeftChild;       public Node ... Read More

How to check whether the tree is symmetric or not using recursion in C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:37:33

224 Views

In the recursive approach we to find a tree is symmetric or not we initially check whether the tree is null or not, if the tree is null then its symmetric, if the tree is not not null we call amethod issymmetricmirror .In isSymmetricMirror we get the value of the left child and right child, if both left and right child are null we consider as symmetric, if either of the value is null then we consider and not symmetric and at last we call the issymmetric method recursively by passing the left and right child values.Examplepublic class TreesPgm{   ... Read More

How to check whether the tree is symmetric or not using iterative in C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:33:25

184 Views

In the Iterative approach we have to create 2 Queues, one queue saves the left child and another queue saves the right child value. If the tree is empty, then it is symmetrical to the vertical axis going through its root node. Else, check if the value at the root node of both subtrees is the same. If it is, then check if the left subtree and the right subtree are symmetrical. Enqueue the left child value and right child value into the queue1 and enqueue the right child and left child value into the queue1Examplepublic class TreesPgm{    public ... Read More

How to implement coin change problem using bottom-up approach using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:30:45

565 Views

CoinChangeBottomUpApproach takes 3 parameters as input n is the amount, coins array contains the total number of coins, t contains total number of coins. Declare a dynamic array which stores the previously calculated values. loop through the array and calculate the minimum coins required to calculate the amount. If the calculation is already done the take the value from the dynamic array.Time complexity − O(N)Space complexity − O(N)Examplepublic class DynamicProgramming{    public int CoinChangeBottomUpApproach(int n, int[] coins, int t){       int[] dp = new int[100];       for (int i = 1; i < n; i++){   ... Read More

How to implement coin change problem using topDown approach using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:25:03

1K+ Views

CoinChangeTopDownApproach takes 4 parameters, n is the amount, coins array contains the coins from which the amount needs to be calculated, t is the total number of coins, dp array will store all the pre calculated values. If amount is 0 then return 0. If the value is already calculated then return from the dp array. if the value is not calculated then call the CoinChangeTopDownApproach recursively.Time complexity − O(N)Space complexity − O(N)Examplepublic class DynamicProgramming{    public int CoinChangeTopDownApproach(int n, int[] coins, int t, int[] dp){       if (n == 0){          return 0;   ... Read More

How to implement minimum step to one using bottom-up approach using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:22:48

227 Views

MinimumStepstoOneBottomdownApproachtakes integer n as input. Parameter n contains the total number of elements. Initial condition checks whether n is equal to 1. If n is equal to 1 then return 0. Initialize op1, op2 and op3 to max value. If n mod 3 is equal to 0 then call MinimumStepstoOneBottomdownApproach recursively and assign it to op1, if n mod 3 is equal to 0 then call MinimumStepstoOneBottomdownApproach recursively and assign it to op2 else subtract n by 1 and call MinimumStepstoOneBottomdownApproach. Finally return the value from the dp arrayTime complexity − O(N)Space complexity − O(N)Examplepublic class DynamicProgramming{    public int ... Read More

How to implement minimum step to one using topDown approach using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:19:00

153 Views

MinimumStepstoOneTopdownApproach takes integer n and an integer array as input. Parameter n contains the total number of elements. Initial condition checks whether n is equal to 1. If n is equal to 1 then return 0. Initialize op1, op2 and op3 to max value . If n mod 3 is equal to 0 then call MinimumStepstoOneTopdownApproach recursively and assign it to op1 , If n mod 3 is equal to 0 then call MinimumStepstoOneTopdownApproach recursively and assign it to op2 else subtract n by 1 and call MinimumStepstoOneTopdownApproach . Finally call the Math.Min to calculate the minimum of three elements ... Read More

How to implement Fibonacci using bottom-up approach using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:16:26

543 Views

The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. The bottom-up approach first focuses on solving the smaller problems at the fundamental level and then integrating them into a whole and complete solution.Time complexity − O(N)Space complexity − O(N)Examplepublic class DynamicProgramming{    public int fibonacciBottomupApproach(int n){       int[] dpArr = new int[150];       dpArr[1] = 1;       for (int i = 2; i

How to implement Fibonacci using topDown approach using C#?

Nizamuddin Siddiqui
Updated on 17-Aug-2021 08:13:23

840 Views

The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. The top-down approach focuses on breaking down a big problem into smaller and understandable chunks. Space complexity is O(N) because we are creating an extra array memory which is equal to the size of number.Time complexity − O(N)Space complexity − O(N)Examplepublic class DynamicProgramming{    public int fibonacciTopdownApproach(int n, int[] dpArr ){       if(n==0 || n ... Read More

Advertisements