# Category Archives: Trees

## Print extreme nodes of each level of Binary Tree in alternate orderJuly 22, 2016

Given a binary tree, print nodes of extreme corners of each level but in alternate order. Example: For above tree, the output can be 1… Read More »

## Print cousins of a given node in Binary TreeJuly 3, 2016

Given a binary tree and a node, print all cousins of given node. Note that siblings should not be printed. Example: Input : root of… Read More »

## Iterative function to check if two trees are identicalJune 26, 2016

Two trees are identical when they have same data and arrangement of data is also same. To identify if two trees are identical, we need… Read More »

## Find largest subtree having identical left and right subtreesJune 17, 2016

Given a binary tree, find the largest subtree having identical left and right subtree. Expected complexity is O(n). For example, Input: 50 / \ 10… Read More »

## Construct a Binary Tree from Postorder and InorderMay 26, 2016

Given Postorder and Inorder traversals, construct the tree. Examples: Input : in[] = {2, 1, 3} post[] = {2, 3, 1} Output : Root of… Read More »

## Check if two trees are MirrorMay 23, 2016

Given two Binary Trees, write a function that returns true if two trees are mirror of each other, else false. For example, the function should… Read More »

## Create a Doubly Linked List from a Ternary Tree

Given a ternary tree, create a doubly linked list out of it. A ternary tree is just like binary tree but instead of having two… Read More »

## Print Binary Tree in 2-DimensionsMay 19, 2016

Given a Binary Tree, print it in two dimension. Examples: Input : Pointer to root of below tree 1 / \ 2 3 / \… Read More »

## Sink Odd nodes in Binary TreeMay 18, 2016

Given a Binary Tree having odd and even elements, sink all its odd valued nodes such that no node with odd value could be parent… Read More »

## Number of ways to traverse an N-ary treeMay 12, 2016

Given an n-ary tree, count number of ways to traverse an n-ary (or a Directed Acyclic Graph) tree starting from the root vertex. Suppose we… Read More »

## Density of Binary Tree in One TraversalMay 9, 2016

Given a Binary Tree, find density of it by doing one traversal of it. Density of Binary Tree = Size / Height Examples: Input: Root… Read More »

## Convert a Binary Tree into Doubly Linked List in spiral fashionMay 8, 2016

Given a Binary Tree, convert it into Doubly Linked List where the nodes are represented Spirally. The left pointer of the binary tree node should… Read More »

## Non-recursive program to delete an entire binary treeMay 7, 2016

We have discussed recursive implementation to delete an entire binary tree here. We strongly recommend you to minimize your browser and try this yourself first. Now… Read More »

## Print root to leaf paths without using recursionMay 4, 2016

Given a binary tree, print all its root to leaf paths without using recursion. For example, consider the following Binary Tree. 6 / \ 3… Read More »

## Diagonal Traversal of Binary TreeMay 3, 2016

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 »