Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
Program to find out the lowest common ancestor of a binary tree using parent pointers using Python
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 MoreProgram to find out the lowest common ancestor of a binary tree using Python
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 MoreProgram to add two polynomials given as linked lists using Python
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 MoreProgram to build and evaluate an expression tree using Python
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 MoreProgram to find out if two expression trees are equivalent using Python
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 MoreProgram to find out the node in the right in a binary tree using Python
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 MoreProgram to find latest group of size M using Python
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 MoreProgram to find minimum numbers of function calls to make target array using Python
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 MoreProgram to find minimum number of vertices to reach all nodes using Python
Suppose we have a directed acyclic graph with n vertices numbered from 0 to n-1. The graph is represented by an edge list, where edges[i] = (u, v) represents a directed edge from node u to node v. We need to find the smallest set of vertices from which all nodes in the graph are reachable. The key insight is that nodes with no incoming edges cannot be reached from any other nodes, so they must be included in our starting set. Example Graph 0 ...
Read MoreProgram to find minimum operations to make array equal using Python
Suppose we have a value n, consider an array nums with n elements, where arr[i] = (2*i)+1 for all i. Now in one operation, we can choose two indices x and y where 0 = 0, add (n-j) to ans, decrement q, and increment j by 2 Return ans Example def solve(n): ans = 0 if n == 1: return ans q = (n // 2) - 1 j = 1 ...
Read More