# Category: Trees

## Connect nodes at the same level in a binary tree

PSEUDO ALGORITHM (Breadth first search approach) We will use the same algorithm discussed in level-order-breadth-first-search-or-zig-zag-traversal-of-a-binary-tree with slight modification. Create an empty queue q Initialize roots height = 0; Enqueue q with the root node Loop...

## Level order, breadth first search or zig-zag traversal of a binary tree

Traverse the binary tree in breadth first search also known as level order traversal manner. // Should print  8, 3, 10, 1, 6, 14, 4, 7, 13 Pseudo Algorithm PSEUDO ALGORITHM (Breadth first search...

## Delete all the nodes from a binary tree

Recursive : To delete all the nodes, you will have to visit all the nodes. There are three ways to traverse the binary tree : Post order Pre order In order. To delete all...

## Print nodes at k level from the root in a binary tree

[et_pb_section admin_label=”section”][et_pb_row admin_label=”row”][et_pb_column type=”4_4″][et_pb_text admin_label=”Text” background_layout=”light” text_orientation=”left” use_border_color=”off” border_color=”#ffffff” border_style=”solid”] Given a binary tree, level k, print all the nodes at level k. For example, 4, 7, 8 are at level 2.   1...

## Find the first common ancestor of two nodes in a binary tree

Algorithm Create two arrays with in-order and post-order traversal for the given binary tree and call them in-order and post-order traversal respectively. Find the number of nodes in-between the given two nodes in the...

## Find the in-order successor of a given node in a binary search tree

Note: We can do in-order traversal, which will give us a sorted array and then find the in-order successor very easily. But the time complexity of in-order traversal in O(n). Where as finding the...

## Create a bar chart from json

Problem : Given a JSON data structure with key representing age and value representing the number of people with that age. For example, { 25: 55, 26: 45, 27: 10, 28: 20, 30: 1,...

## Determine if a binary tree is a binary search tree

Binary tree and Binary search tree are defined as follows : Binary tree is a tree data structure in which each node has at most two child nodes. A binary search tree (BST) is...

## Flatten binary tree to linked list

Problem description : Convert a given binary tree to a linked list of all the nodes at each depth (if you have a binary tree with depth D, you’ll have D linked lists). Logic...

## Create a minimal height binary search tree from given sorted array of unique integers

Problem description : Given a sorted (increasing order) array with unique integer elements, create a binary search tree (BST) with minimal height. Input : A sorted array with unique integer elements // [0, 1,...