Problem Statement: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example:

Code:

Approach-1: BFS

Explanation:

Initialize a queue data structure that would be useful for tree traversal and storing the nodes of the tree level by level. Initially add root to the queue. Then iterate over each level of the tree.

At each level, the last node of the level (i.e, the rightmost node) would be viewed from the right side.

Iterate over all the nodes on the current level…


Problem Statement: You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL.

Example:

Code:

Explanation:

Above is the iterative approach to the problem.

Initialize a queue data structure that would be useful for tree traversal. Initially add root to the queue. Then iterate over each level of the tree.

Iterate over all the nodes…


Problem Statement: Given an undirected graph , return true if and only if it is bipartite.

A graph is bipartite if we can split its set of nodes into two independent subsets A and B such that every edge in the graph has one node in A and another node in B.

Example:

In the above examples, graph[i] is a list of indexes j for which the edge between nodes i and j exists. Each node is an integer between 0 and graph.length - 1.

Code:

Explanation:

Above approach is the graph colouring using DFS.

Create an array of…


Problem Statement: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.

LCA is defined between two nodes p and q as the lowest node in the tree that has both p and q as descendants (where a node can be a descendant to itself).

Example:

Code:

Explanation:

Above is the iterative approach to the problem.

Starting from the root node, we need to maintain HashMap to store the parent pointers of each node until we find both p and q (for the root node, keep null as the parent pointer).

Once…


Problem Statement: Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference of the BST.

BST is a tree in which every node in the left subtree have value lesser than the root node and nodes in the right subtree have value greater than the root node.

To delete a node in a BST:

  1. Find the node to be deleted.
  2. Remove it and replace it with its successor/predecessor and update the BST.

Example:

Code:

Explanation:

Recursive Approach:

Three cases:

  1. Key is in the…

Shivani Dwivedi

Software Engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store