## Print the longest leaf to leaf path in a Binary tree

The diameter of a tree (sometimes called the width) is the number of nodes on the longest path between two end nodes. In this post,… Read More »

## Find if given vertical level of binary tree is sorted or not

Given a binary tree. Find if a given vertical level of the binary tree is sorted or not. (For the case when two nodes are… Read More »

## Traversal of tree with k jumps allowed between nodes of same height

There is a tree with N nodes and node 1 is the root node. Each node of the tree can contain fruit or not. Initially,… Read More »

## BST to a Tree with sum of all smaller keys

Given a Binary Search Tree(BST), convert it to a Binary Tree such that every key of the original BST is changed to key plus sum… Read More »

## Number of children of given node in n-ary Tree

Given a node x, find the number of children of x(if it exists) in the given n-ary tree. Example : Input : x = 50… Read More »

## Disjoint Set Union on trees | Set 2

Given a tree, and the cost of a subtree is defined as |S|*AND(S) where |S| is the size of the subtree and AND(S) is bitwise… Read More »

## Disjoint Set Union on trees | Set 1

Given a tree and weights of nodes. Weights are non-negative integers. Task is to find maximum size of a subtree of a given tree such… Read More »

## Number of siblings of a given Node in n-ary Tree

Given an N-ary tree, find the number of siblings of given node x. Assume that x exists in the given n-ary tree. Example : Input… Read More »

## Count elements which divide all numbers in range L-R

Given N numbers and Q queries, each query consists of L and R. Task is to write a program which prints the count of numbers… Read More »

## Find largest subtree sum in a tree

Given a binary tree, task is to find subtree with maximum sum in tree. Examples: Input : 1 / \ 2 3 / \ /… Read More »

## Two Dimensional Segment Tree | Sub-Matrix Sum

Given a rectangular matrix M[0…n-1][0…m-1], and queries are asked to find the sum / minimum / maximum on some sub-rectangles M[a…b][e…f], as well as queries… Read More »

## Combinatorics on ordered trees

An ordered tree is an oriented tree in which the children of a node are somehow ordered. It is a rooted tree in which an… Read More »

## Euler Tour | Subtree Sum using Segment Tree

Euler tour tree (ETT) is a method for representing a rooted tree as a number sequence. When traversing the tree using Depth for search(DFS), insert… Read More »

## Range and Update Query for Chessboard Pieces

Given N pieces of chessboard all being ‘white’ and a number of queries Q. There are two types of queries : Update : Given indices… Read More »

## Level order traversal with direction change after every two levels

Given a binary tree, print the level order traversal in such a way that first two levels are printed from left to right, next two… Read More »