Found 26504 Articles for Server Side Programming

Program to change the root of a binary tree using Python

Arnab Chakraborty
Updated on 29-May-2021 13:24:48

901 Views

Suppose, we are given a binary tree and a node that is situated at the leaf of the binary tree.We have to make the leaf node the root node of the binary tree. We can do it in the following way −If a node has a left child, it becomes the right child.A node's parent becomes its left child. In this process, the parent node's link to that node becomes null, so it will have only one child.The node structure of the tree is like below −TreeNode:    data:    left:    right:    parent: We have ... Read More

Program to fix a erroneous binary tree using Python

Arnab Chakraborty
Updated on 29-May-2021 13:27:08

225 Views

Suppose, we are given a binary tree that has a problem; one of the node's right child pointer points to another node at the same level in the binary tree erroneously. So, to fix this problem, we have to find out the node that has this error and delete that node and its descendants except the node that it is erroneously pointing to. We return the root node of the fixed binary tree.So, if the input is likeWe can see that there is an erroneous link between 4 and 6. The right child pointer of 4 points to 6.then the ... Read More

Program to find out the lowest common ancestor of a binary tree using parent pointers using Python

Arnab Chakraborty
Updated on 29-May-2021 13:35:22

191 Views

Suppose, we are given a binary tree and also two specific nodes x and y. We have to find out the lowest common ancestor of the two nodes from the binary tree. The lowest common ancestor in a binary tree is the lowest node of which both the nodes x and y are descendants. A particular node can also be a descendant of itself. We have to find the node and return it as an output.The node structure of the tree is like below −TreeNode:    data:    left:    right:    parent: We have to utilize ... Read More

Program to find out the lowest common ancestor of a binary tree using Python

Arnab Chakraborty
Updated on 28-May-2021 14:02:53

157 Views

Suppose, we are given a binary tree and also two specific nodes x and y. We have to find out the lowest common ancestor of the two nodes from the binary tree. The lowest common ancestor in a binary tree is the lowest node of which both the nodes x and y are descendants of. Also, a particular node can also be a descendant of itself. We have to find the node and return it as an output.So, if the input is likeand x = 2, y = 4; then the output will be 3.The node of which the nodes ... Read More

Program to add two polynomials given as linked lists using Python

Arnab Chakraborty
Updated on 28-May-2021 14:04:57

3K+ Views

Suppose, we are given two polynomials and we have to find out the addition of the two polynomials. The polynomials have to be represented as linked lists; the terms of the polynomials will be represented as a linked list node. Each linked list node will contain the coefficient value, power value, and the pointer to the next linked list node. We have to return a third linked list which is the addition of two linked list polynomials.So, if the input is like1x^1 + 1x^2 = 0 and 2x^1 + 3x^0 = 0, then the output will be 3x^1 + 1x^2 ... Read More

Program to build and evaluate an expression tree using Python

Arnab Chakraborty
Updated on 28-May-2021 14:03:32

2K+ Views

Suppose, we are given the post order traversal of an expression tree. We have to build an expression tree from the given post-order traversal, and then evaluate the expression. We return the root of the expression tree and the evaluated value of the tree.So, if the input is likethen the output will be -7.The postfix order given as input of the tree is ['1', '2', '+', '3', '4', '+', '*']. The expression if evaluated, becomes (1 – 2) * (3 + 4); which equals -7.To solve this, we will follow these steps −LEFT = 0 RIGHT = 1Define a function ... Read More

Program to find out if two expression trees are equivalent using Python

Arnab Chakraborty
Updated on 29-May-2021 13:56:21

230 Views

Suppose, we are provided two expression trees. We have to write a program that checks the two expression trees and determines if the expression trees generate similar values. The two expression trees are provided to us in an in-order manner and we return a True value if they match, or else we return a False value.So, if the input is likethen the output will be True.The two expression trees evaluate to the same value.To solve this, we will follow these steps:Define a function dfs() . This will take node, dicif node is empty, thenreturnif left of node and right of ... Read More

Program to find out the node in the right in a binary tree using Python

Arnab Chakraborty
Updated on 29-May-2021 13:37:28

517 Views

Suppose, we are provided a binary tree. We are also given a pointer to a node (named ‘u’) and we have to find the node situated just right of the provided node. The node situated to the given node's right must stay at the same level and the given node can either be a leaf node or an internal node.So, if the input is likeand u = 6, then the output will be 8.The node situated at the right of node 6 is node 8, so the value 8 is returned to us.To solve this, we will follow these steps ... Read More

Program to find latest group of size M using Python

Arnab Chakraborty
Updated on 29-May-2021 13:41:33

196 Views

Suppose we have an array arr this is holding a permutation of numbers from 1 to n. If we have a binary string of size n and initially all its bits set to zero. Now at each step i (Indexing starts from 1 for both the binary string and arr) from 1 to n, the bit at position arr[i] is set to 1. We also have another value m, and we need to find the latest step at which there exists a group of ones of size m. Here a group of ones means a contiguous substring of 1s such ... Read More

Program to find minimum numbers of function calls to make target array using Python

Arnab Chakraborty
Updated on 29-May-2021 13:43:10

300 Views

Suppose we have following function definition:def modify(arr, op, index):    if op == 0:       arr[index] += 1    if op == 1:       for i in range(len(arr)):          arr[i] *=2We have to find minimum number of function calls required to make a given array nums from one zero array of same size?So, if the input is like nums = [1, 5, 3], then the output will be 7 because, initially all elements are 0, [0, 0, 0]At first step increase second element by 1, so array is [0, 1, 0]Double second element ... Read More

Advertisements