# Tagged: Trees

## Double Threaded Binary Tree Complete Implementation

In earlier article “Introduction to Threaded Binary Tree” we have seen what is threaded binary tree, types of it and what advantages it has over normal binary tree. In this article we will see...

## Single Threaded Binary Tree Complete Implementation

In earlier article “Introduction to Threaded Binary Tree” we have seen what is threaded binary tree, types of it and what advantages it has over normal binary tree. In this article we will see...

## Introduction to Threaded Binary Tree

What is Threaded Binary Tree?? A binary tree is threaded by making all right child pointers that would normally be null point to the inorder successor of the node (if it exists), and all left child pointers...

## Convert BST to Greater Sum Tree

Objective: Given a binary search tree (BST), convert it into greater sum tree. What is greater sum tree: Greater sum tree is a tree in which every node contains the sum of all the...

## Get the Sum of all left leaves in a Binary tree

Objective: Given a binary tree, find the sum of all the nodes which are left as well as leaves nodes. Example:     Approach: Approach is quite simple. Do the inorder traversal check if...

## Convert binary tree to its Sum tree

Objective: Given a binary tree, write an algorithm to convert it into its Sum tree. What is Sum tree: Sum tree of a binary tree, is a tree where each node in the converted...

## Binary Tree-Postorder Traversal – Non Recursive Approach

Objective: Given a binary tree, write a non recursive or iterative algorithm for postorder traversal. Example: Earlier we have seen “What is postorder traversal and recursive algorithm for it“, In this article we will...

## Binary Tree – Preorder Traversal – Non Recursive Approach

Objective: Given a binary tree, write a non recursive or iterative algorithm for preorder traversal. Example: Earlier we have seen “What is preorder traversal and recursive algorithm for it“, In this article we will...

## Binary Tree-Inorder Traversal – Non Recursive Approach

Objective: Given a binary tree, write a non recursive or iterative algorithm for Inorder traversal. Example: Earlier we have seen “What is Inorder traversal and recursive algorithm for it“, In this article we will...

## Delete the Binary Tree

Objective: Given a binary tree, write an algorithm to delete it. This is one of the basic problem in trees. if you are new to trees then this problem will help you build your...

## Search the Element in a binary tree – With and Without Recursion

Objective: Given a binary tree and a given number x, Write an recursive algorithm to search the element in the tree. This is one of the very basic problems of tree. If you are...

## Tree Traversals

There are multiple ways to in which you can traverse a tree. In this article we will see these traversals in detail. If you are new to trees then I would recommend that you...

## Find the Size of a Binary Tree without Recursion

Objective: Given a binary tree, Write an non-recursive algorithm to find the size of the tree. Note : Size of the tree is num­ber of nodes in the tree Approach: In our earlier post (link)...

## Breadth-First Search/Traversal in a Binary Tree

Breadth-First Search ( or Traversal) also know as Level Order Traversal. What is Breadth First Search: Breadth-first search (BFS) is an algorithm for traversing or searching tree or graph data structures. It starts at...

## Merge K Sorted Arrays

Objective: Given k sorted array, write an algorithm to merge Them into One sorted array. Example : int[][] A = new int[5][]; A[0] = new int[] { 1, 5, 8, 9 }; A[1] =...

## Find the Max element in a Given Binary Tree

Objective: – Given a binary tree , Find the max element in it. Example: Approach: Use Recursion. Max will the Max(root, max element in left subtree, max element in rightsubtree) Recursively solve for max...

## Provide the Next Siblings Pointers in a Given Binary Tree

Objective: – Given a binary tree with three pointers left, right and nextSibling). Write the program to provide the nextsibling pointers. Example: Approach:

## Check If One Binary is Mirror Tree of another Binary Tree.

Objective: – Given two binary trees check if they are mirror image of each other. Example: Approach:

## Print All Paths From Root In a Binary Tree Whose Sum is Equal to a Given Number

Objective: – Given a binary tree and X, Print all the paths starting from root so that sum of all the nodes in path equals to a given number. Example:

## Diameter Of a Binary Tree

Objective: – Given a binary tree, find the diameter of it. What is Diameter Of a Tree: Diameter of tree is defined as A longest path or route between any two nodes in a...

## Reverse Level Order Traversal

Objective: – Given a binary tree, Do the reverse level order traversal. In our earlier post we have seen normal Level Order Traversal. In reverse level order traversal we first need to print the...

## Find the Deepest Node in a Binary Tree.

Objective: – Given a binary tree, Find the deepest node in it. Approach: Take two global variable as “deepestlevel” and “value“. starting with level=0, Do the inorder traversal and whenever you go down one...

## Find the Height of a tree without Recursion

Objective: – Find the Height of a tree without Recursion. In our earlier post “Height of tree” we had used recursion to find it. In this post we will see how to find it...

## Print All The Full Nodes in a Binary Tree

Objective: Given a binary tree, print all nodes will are full nodes. Full Nodes: Nodes Which has both the children, left and right are called Full Nodes Approach: quite simple Solution. Do the any...

## Binary Min – Max Heap

A binary heap is a heap data structure created using a binary tree. binary tree has two rules – Binary Heap has to be complete binary tree at all levels except the last level....

## Print the Bottom View of the Binary Tree.

Objec­tive: – Given a binary tree, print it in Bottom View of it. What is Bottom View: Bottom view means when you look the tree from the bottom the nodes you will see will be...

## AVL Tree – Insertion

What is AVL Tree : AVL tree is widely known as self-balancing binary search tree. It is named after its creator (Georgy Adelson-Velsky and Landis’ tree). In AVL Tree, the heights of child subtrees...

## Construct a binary tree from given Inorder and Level Order Traversal

Objective: – Given a inorder and level order traversal, construct a binary tree from that. Input: Inorder and level order traversal Appraoch: int[] inOrder = { 4, 2, 5, 1, 6, 3, 7 };...

## Construct Binary Search Tree from a given Preorder Traversal Using Stack (Without Recursion)

Objective: – Given a preorder traversal, construct BST from that, without using recursion. Input: Preorder traversal Similar Problem : This problem is similar to the Construct Binary Search Tree from a given Preorder Traversal...

## Construct Binary Search Tree from a given Preorder Traversal using Recursion

Objective: – Given a preorder traversal, construct BST from that. Input: Preorder traversal Similar Problem : This problem is similar to the – Construct Binary Search Tree from a given Preorder Traversal Using Stack...

## Print The Top View of a Binary Tree

Objective: – Given a binary tree, print it in Top View of it. What is Top View: Top view means when you look the tree from the top the nodes you will see will...

## Construct a Binary Tree from Given Inorder and Depth-First-Search.

Objective: – Given a inorder and pre­order tra­ver­sal, con­struct a binary tree from that. Input: Inorder traversal and Depth-First-Search. Approach: int[] inOrder = { 8, 4, 2, 5, 1, 6, 3, 7, 9 }; int[]...

## Depth First Search/Traversal in Binary Tree

Objective: – Given a Binary Search Tree, Do the Depth First Search/Traversal . Appraoch: Approach is quite simple, use Stack. First add the add root to the Stack. Pop out an element from Stack...

## Inorder Predecessor and Successor in Binary Search Tree

Objective: – Given a Binary Search Tree, Find predecessor and Successor of a given node. What is Predecessor and Successor : When you do the inorder traversal of a binary tree, the neighbors of...

## Print All The Nodes Which are X distance from the Given Node

Objective: – Given Binary Tree, Print All The Nodes Which are X distance from the Given Node. Example : Appraoch: Quite Tricky solution, i will explain using the example given in the picture.

## Print All The Nodes Which are X distance from the Leaf Nodes

Objective: – Given Binary Tree, Print All The Nodes Which are X distance from the Leaf Nodes Example : Approach:

## Print All The Nodes Which are X distance from the Root

Objective: – Given Binary Tree, Print all the nodes which are X distance from the root Example : Appraoch:

## Find the Distance between Two Nodes of a Binary Tree.

Objective: – Given nodes in a binary tree, find the distance between them. Example : Approach:

## Find The Distance From Root To Given Node of a Binary Tree.

Objective: – Find The Distance From Root To Given Node of a binary tree. What does Distance means : It means number of edges between two nodes. Approach:

## Construct a binary tree from given Inorder and Postorder Traversal

Objective: – Given a inorder and postorder traversal, write an algorithm to construct a binary tree from that. This problem was asked in the Microsoft coding competition. Input: Inorder and postorder traversals Similar Problems:...

## Print the Vertical Sum in binary Tree .

Objective: – Given a binary tree, print it in vertical order sum What is Vertical Order Sum as you can see in the example above, [4],[2], [12],[3],[7] are the vertical order sum of the...

## Print the Binary Tree in Vertical Order Path.

Objective: – Given a binary tree, print it in vertical order path. What is Vertical Order as you can see in the example above, [4],[2], [1,5,6],[3],[7] are the verical order of the given binary...

## Lowest Common Ancestor in a Binary Tree (Not Binary Search Tree).

Objective: – Find the Lowest Common Ancestor of two given nodes in a Binary Tree What is Lowest Common Ancestor In a given binary tree, The lowest common ancestor of two nodes n1 and...

## Lowest Common Ancestor in a Binary Search Tree.

Objective: – Find the Lowest Common Ancestor of two given nodes in a Binary Search Tree What is Lowest Common Ancestor In a given binary tree, The lowest common ancestor of two nodes n1...

## Make a Binary Tree from Given Inorder and Preorder Traveral.

Objective: – Given a inorder and preorder traversal, construct a binary tree from that. Input: Inorder and preorder traversals Similar Problem: Construct a binary tree from given Inorder and Postorder Traversal Approach: int []...

## Given a binary tree, find out the maximum sum of value from root to each leaf.

Objective: – Find the maximum sum leaf to root path in a Binary Tree. Means in all the paths from root to leaves, find the path which has the maximum sum. Input: A binary...

## Reverse Alternate levels of a given Binary Tree.

Objective: – Reverse Alternate levels of a given binary tree Input: A binary tree Example: Appraoch:

## Given a Sorted Singly Linked List Array, Convert it into a Balanced Binary search Tree.

Objective: You have been given a sorted singly List, you need to convert it into balanced binary search tree. Why balanced binary tree is important: You can also create first node as root and...

## Given a binary tree, Convert it into its Mirror Tree

Objective: Given a binary tree, Convert it into its Mirror Tree Mirror Tree: Mirror Tree of a binary tree is where left and right child of every node of given binary tree is interexchanged....

## Given a binary tree, Print All the Nodes that don’t have Siblings.

Objective: Given a binary tree, Print All the Nodes that don’t have siblings. Note: sibling node is the node which has the same parent, so you need to print the nodes who is a...