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 »

# Posts By: admin

### Priority Queues in Java

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 »

### Average of Levels in Binary Tree

Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array. Example 1: Input: 3 / \ 9 20 / \ 15 7 Output: [3, 14.5, 11] Explanation: The average value of nodes on level 0 is 3, on level 1 is 14.5, and on… Read more »

### Binary Search Trees

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

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

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 »