Server Side Programming Articles

Page 366 of 2109

Program to change the root of a binary tree using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Suppose we are given a binary tree and need to change its root to a specific leaf node. This transformation involves restructuring the tree by following specific rules to maintain the binary tree structure while making the leaf node the new root. Transformation Rules To change the root of a binary tree, we follow these rules − 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. ...

Read More

Program to fix a erroneous binary tree using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 310 Views

When a binary tree has an erroneous connection where a node's right child pointer incorrectly points to another node at the same level, we need to identify and fix this error. The solution involves detecting the problematic node and removing it along with its descendants, except for the node it erroneously points to. Consider this example where node 4's right child incorrectly points to node 6: 5 3 7 ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 280 Views

The Lowest Common Ancestor (LCA) of two nodes in a binary tree is the lowest node that has both nodes as descendants. When parent pointers are available, we can efficiently find the LCA by traversing upward from one node and checking if we encounter the other node's ancestors. Node Structure The tree node contains parent pointers for upward traversal ? TreeNode: data: left: right: parent: Algorithm The approach follows these steps ? ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 282 Views

The Lowest Common Ancestor (LCA) of two nodes in a binary tree is the deepest node that has both nodes as descendants. A node can be considered a descendant of itself. This problem is commonly solved using recursive traversal. So, if the input is like: 5 3 7 2 ...

Read More

Program to add two polynomials given as linked lists using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 3K+ Views

Suppose we are given two polynomials represented as linked lists and we need to find their addition. Each polynomial is stored as a linked list where each node contains a coefficient, power, and pointer to the next node. We need to return a new linked list representing the sum of the two polynomials. For example, if we have polynomials 1x^1 + 1x^2 and 2x^1 + 3x^0, then the output will be 3x^1 + 1x^2 + 3x^0. Algorithm To solve this, we will follow these steps − Create dummy and node pointers ...

Read More

Program to build and evaluate an expression tree using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 2K+ Views

Suppose, we are given the postfix traversal of an expression tree. We have to build an expression tree from the given postfix 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 like: * + + ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 322 Views

When working with expression trees, we sometimes need to determine if two trees represent equivalent expressions. This involves checking whether both trees evaluate to the same result, even if their structure differs due to commutative properties of operations. Expression trees store mathematical expressions where leaf nodes contain operands and internal nodes contain operators. Two trees are equivalent if they produce the same value when evaluated. .node { fill: lightblue; stroke: black; stroke-width: 2; } .leaf { fill: ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 602 Views

Sometimes we need to find the node immediately to the right of a given node in a binary tree. The right node must be at the same level as the target node. This problem can be solved using level-order traversal (BFS) to process nodes level by level. So, if the input is like 5 3 7 2 ...

Read More

Program to find latest group of size M using Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 280 Views

Suppose we have an array arr holding a permutation of numbers from 1 to n. We have a binary string of size n with all bits initially set to zero. At each step i (1-indexed), we set the bit at position arr[i] to 1. Given a value m, we need to find the latest step at which there exists a group of ones of size exactly m. A group of ones means a contiguous substring of 1s that cannot be extended in either direction. If no such group exists, we return -1. Example Walkthrough If the input ...

Read More

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 390 Views

Suppose we have a function that can perform two operations on an array: def modify(arr, op, index): if op == 0: arr[index] += 1 if op == 1: for i in range(len(arr)): arr[i] *= 2 We need to find the minimum number of function calls required to transform a zero array into a given target array nums. Problem Understanding ...

Read More
Showing 3651–3660 of 21,090 articles
« Prev 1 364 365 366 367 368 2109 Next »
Advertisements