Category Archives: Trees

Find n-th node of inorder traversalNovember 20, 2017

iven the binary tree and you have to find out the n-th node of inorder traversal. Examples: Input : n = 4 10 / \… Read More »

Diameter of n-ary tree using BFSNovember 16, 2017

N-ary tree refers to the rooted tree in which each node having atmost k child nodes. The diameter of n-ary tree is the longest path… Read More »

Iterative diagonal traversal of binary treeNovember 15, 2017

Consider lines of slope -1 passing between nodes. Given a Binary Tree, print all diagonal elements in a binary tree belonging to same line. Input… Read More »

Delete leaf nodes with value as xNovember 11, 2017

Given a binary tree and a target integer x, delete all the leaf nodes having value as x. Also, delete the newly formed leaves with… Read More »

Binary tree to string with bracketsNovember 10, 2017

Construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way. The null node needs to be represented by… Read More »

Ways to color a skewed tree such that parent and child have different colors

Given a skewed tree (Every node has at most one child) with N nodes and K colors. You have to assign a color from 1… Read More »

Root to leaf path with maximum distinct nodesNovember 9, 2017

Given a Binary Tree, find count of distinct nodes in a root to leaf path with maximum distinct nodes. Examples: Input : 1 / \… Read More »

Construct Binary Tree from String with bracket representationNovember 6, 2017

Construct a binary tree from a string consisting of parenthesis and integers. The whole input represents a binary tree. It contains an integer followed by… Read More »

Check if the given array can represent Level Order Traversal of Binary Search TreeOctober 23, 2017

Given an array of size n. The problem is to check whether the given array can represent the level order traversal of a Binary Search… Read More »

Replace node with depth in a binary treeOctober 17, 2017

Given a binary tree, replace each node with its depth value. For example, consider the following tree. Root is at depth 0, change its value… Read More »

Decision TreeOctober 16, 2017

Decision Tree : Decision tree is the most powerful and popular tool for classification and prediction. A Decision tree is a flowchart like tree structure,… Read More »

Number of Binary Trees for given Preorder Sequence lengthOctober 14, 2017

Count the number of Binary Tree possible for a given Preorder Sequence length n. Input : n = 1 Output : 1 Input : n… Read More »

Number of nodes greater than a given value in n-ary treeOctober 12, 2017

Given a n-ary tree and a number x, find and return the number of nodes which are greater than x. Example: In the given tree,… Read More »

Next Larger element in n-ary tree

Given a generic tree and a integer x. Find and return the node with next larger element in the tree i.e. find a node just… Read More »

Second Largest element in n-ary treeOctober 11, 2017

Given an N-ary tree, find and return the node with second largest value in the given tree. Return NULL if no node with required value… Read More »