All Nodes Distance K in Binary Tree

Posted by & filed under Programming.

We are given a binary tree (with root node root), a target node, and an integer value K. Return a list of the values of all nodes that have a distance K from the target node.  The answer can be returned in any order.   Example 1: Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2 Output: [7,4,1] Explanation: The nodes that are… Read more »

Priority Queues in Java

Posted by & filed under Programming, Queue.

Priority queues are a generalisation of stacks and queues. Rather than inserting and deleting elements in a fixed order, each element is assigned a priority represented by an integer. We always remove an element with the highest priority, which is given by the minimal integer priority assigned. Priority queues often have a fixed size. Advantages… Read more »

Binary Search Trees

Posted by & filed under Binary Search, Programming, Tree.

Binary Search Trees or BST is an ordered binary tree where all the nodes on the left of a node are less or equal to the node’s own value and all the values on the right are greater or equal to the node’s own value. A Binary Tree is a tree with only two children,… Read more »

Breadth First Search or BFS for a Graph

Posted by & filed under Programming.

In Breadth-first search (BFS) you start with the root, move left to right across the second level, then move left to right across the third level, and so forth. Breadth-first search (BFS) is a method for exploring a tree or graph. In a BFS, you first explore all the nodes one step away, then all… Read more »

How to Build a Graph from a Binary Tree using Depth First Search – DFS

Posted by & filed under Depth First Search DFS, Graphs, Java, Programming.

If we are given a binary tree (with the root node), we can build a tree map without generating extra edges by using this helper method: it is an undirected graph with no weight/weight value of 1 the method of drawing is done using Depth First Search – DFS Map<TreeNode, List<TreeNode>> graph = new HashMap<>();… Read more »