🌐
HackerRank
hackerrank.com › challenges › kruskalmstrsub › problem
Kruskal (MST): Really Special Subtree | HackerRank
There is only one exclusive path from a node to every other node. The subgraph is of minimum overall weight (sum of all edges) among all such subgraphs. ... To create the Really Special SubTree, always pick the edge with smallest weight.
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › number-of-special-nodes-in-an-n-ary-tree
Number of special nodes in an n-ary tree - GeeksforGeeks
August 6, 2021 - Input: val[] = {1, 2, 3, 4, 5, 7, 2, 3} 1 / \ 2 3 / \ \ 4 5 7 / \ 2 3 Output: 7 All nodes except the leaf node 2 are special.
🌐
HackerRank
hackerrank.com › contests › algoholic-contest-1 › challenges › special-paths
Special Paths | Algoholic Contest #1 Question
Find the number of special paths. Solving code challenges on HackerRank is one of the best ways to prepare for programming interviews.
🌐
HackerRank
hackerrank.com › challenges › binary-search-tree-1 › problem
Binary Tree Nodes | HackerRank
Write a query to find the node type of BST ordered by the value of the node.
🌐
HackerRank
hackerrank.com › challenges › primsmstsub › problem
Prim's (MST) : Special Subtree | HackerRank
One specific node is fixed as the starting point of finding the subgraph using Prim's Algorithm.
🌐
TutorialsPoint
tutorialspoint.com › program-to-find-out-the-special-nodes-in-a-tree-in-python
Program to Find Out the Special Nodes in a Tree in Python
So we have this tree, we have to find out the number of special nodes. So, if the input is like tree = [ [1,2], [0], [0,3], [2] ] ... Define a function check_intersection() . This will take colors, child_colors · if length of (colors) < length of (child_colors) , then ... Define a function dfs() . This will take node, prev ... import collections class Solution: def solve(self, tree, color): self.result = 0 def dfs(node, prev): colors = {color[node]} for child in tree[node]: if child != prev: child_colors = dfs(child, node) if colors and child_colors: if self.check_intersection(colors, child_c
🌐
HackerEarth
hackerearth.com › practice › algorithms › graphs › depth-first-search › practice-problems › algorithm › tree-and-special-node-ecdb1606
Tree And Special node | Practice Problems
Prepare for your technical interviews by solving questions that are asked in interviews of various companies. HackerEarth is a global hub of 5M+ developers. We help companies accurately assess, interview, and hire top developers for a myriad of roles.
🌐
HackerEarth
hackerearth.com › practice › algorithms › graphs › depth-first-search › practice-problems › algorithm › jenny-and-water-7-d0337cc3-ec2c1136
Connecting the special nodes | Practice Problems
Prepare for your technical interviews by solving questions that are asked in interviews of various companies. HackerEarth is a global hub of 5M+ developers. We help companies accurately assess, interview, and hire top developers for a myriad of roles.
Find elsewhere
🌐
GitHub
github.com › alexprut › HackerRank › blob › master › Algorithms › Graph Theory › Kruskal MST Really Special Subtree › Solution.java
HackerRank/Algorithms/Graph Theory/Kruskal MST Really Special Subtree/Solution.java at master · alexprut/HackerRank
Node(int id) { this._id = id; this._members.add(this._id); } · public int getId() { return this._id; } · public void setMembers(HashSet<Integer> m) { this._members = m; } · public HashSet<Integer> getMembers() { return this._members; } · public boolean hasMember(Integer id) { return this._members.contains(id); } } · public class Solution { ·
Author   alexprut
🌐
HackerRank
hackerrank.com › challenges › components-in-graph › problem
Components in a graph | HackerRank
In each edge, the first value will be between and , inclusive. The second node will be between and , inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes.
🌐
YouTube
youtube.com › watch
Binary Tree Nodes | SQL Advanced Select | HackerRank Solution - YouTube
A lesson that teaches you how to solve the following problem from the SQL section in HackerRank. https://www.hackerrank.com/challenges/binary-search-tree-1Le...
Published   September 7, 2023
🌐
HackerRank
hackerrank.com › challenges › swap-nodes-algo › problem
Swap Nodes [Algo] | HackerRank
Given a tree and an integer, k, in one operation, we need to swap the subtrees of all the nodes at each depth h, where h ∈ [k, 2k, 3k,...]. In other words, if h is a multiple of k, swap the left and right subtrees of that level.
🌐
HackerRank
hackerrank.com › challenges › get-the-value-of-the-node-at-a-specific-position-from-the-tail › problem
Get Node Value | HackerRank
Given a pointer to the head of a linked list and a specific position, determine the data value at that position. Count backwards from the tail node.
Top answer
1 of 1
2

One-line takeaway: a tree has either one center or two adjacent centers, which are shared by all diameters.


The clue to solve the problem faster is sort of hidden in the symmetric tree given in the question. You can observe that the node D is the center of the tree. Every endpoint of a diameter is 2 edges away from D.

However, all trees are not symmetric. For example, we can remove node B from the given graph. Well, node D remains to be "the center".

In general, if a diameter of a tree is of even length, that diameter has a node at its center. For example, the diameter
$\quad\quad$ A - C - D - E - G $\quad\quad$
has node D as its center node. Moreover, that center node is the center node of every diameter of the tree. So in this case, we can locate the center. Then breadth-first (level-by-level) search starting from the center. The nodes we will visit at the furthest level will be all endpoints of all diameters.

Another case is when diameters are of odd length. Then each diameter has two nodes in the middle. For example, imagine node F and G are removed. Then node C and D are the center nodes. Moreover, those two center nodes are the centers nodes of every diameter of the tree. In this case, we will locate the centers. Imagine the edge between the two centers is broken. Then we can do two breadth-first (level-by-level) search, each time starting from one of the two centers. The nodes we will visit at the furthest level in either BFS will be all endpoints of all diameters.

Let $n$ be the number of nodes in the tree.

  • It takes $O(n)$ time to find a diameter, either by one DFS or two DFS.
  • It takes $O(n)$ time to locate the center or centers of a diameter.
  • One BFS in the case of one center or Two BFS in the case of the two centers will find all the endpoints of diameters. This takes $O(n)$ time. (Instead, we could also do one DFS, keeping track of the distance and, in the case of two centers, which side of center the node is in. This also takes $O(n)$ time.)

So, the whole algorithm takes $O(n)$ time.


Exercise. Show that all diameters of a tree share their centers, whether there is one center or two centers.