Given a tree, and the weights of all the nodes and an integer x, the task is to find a node i such that |weight[i] – x| is minimum.
Examples:
Input:

x = 15
Output: 3
Node 1: |5 – 15| = 10
Node 2: |10 – 15| = 5
Node 3: |11 -15| = 4
Node 4: |8 – 15| = 7
Node 5: |6 -15| = 9
Approach: Perform dfs on the tree and keep track of the node whose weighted absolute difference with x gives the minimum value.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimum = INT_MAX, x, ans;
vector< int > graph[100];
vector< int > weight(100);
void dfs( int node, int parent)
{
if (minimum > abs (weight[node] - x)) {
minimum = abs (weight[node] - x);
ans = node;
}
for ( int to : graph[node]) {
if (to == parent)
continue ;
dfs(to, node);
}
}
int main()
{
x = 15;
weight[1] = 5;
weight[2] = 10;
weight[3] = 11;
weight[4] = 8;
weight[5] = 6;
graph[1].push_back(2);
graph[2].push_back(3);
graph[2].push_back(4);
graph[1].push_back(5);
dfs(1, 1);
cout << ans;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static int minimum = Integer.MAX_VALUE, x, ans;
@SuppressWarnings ( "unchecked" )
static Vector<Integer>[] graph = new Vector[ 100 ];
static int [] weight = new int [ 100 ];
static
{
for ( int i = 0 ; i < 100 ; i++)
graph[i] = new Vector<>();
}
static void dfs( int node, int parent)
{
if (minimum > Math.abs(weight[node] - x))
{
minimum = Math.abs(weight[node] - x);
ans = node;
}
for ( int to : graph[node])
{
if (to == parent)
continue ;
dfs(to, node);
}
}
public static void main(String[] args)
{
x = 15 ;
weight[ 1 ] = 5 ;
weight[ 2 ] = 10 ;
weight[ 3 ] = 11 ;
weight[ 4 ] = 8 ;
weight[ 5 ] = 6 ;
graph[ 1 ].add( 2 );
graph[ 2 ].add( 3 );
graph[ 2 ].add( 4 );
graph[ 1 ].add( 5 );
dfs( 1 , 1 );
System.out.println(ans);
}
}
|
Python3
from sys import maxsize
def dfs(node, parent):
global minimum, graph, weight, x, ans
if minimum > abs (weight[node] - x):
minimum = abs (weight[node] - x)
ans = node
for to in graph[node]:
if to = = parent:
continue
dfs(to, node)
if __name__ = = "__main__" :
minimum = maxsize
graph = [[] for i in range ( 100 )]
weight = [ 0 ] * 100
x = 15
ans = 0
weight[ 1 ] = 5
weight[ 2 ] = 10
weight[ 3 ] = 11
weight[ 4 ] = 8
weight[ 5 ] = 6
graph[ 1 ].append( 2 )
graph[ 2 ].append( 3 )
graph[ 2 ].append( 4 )
graph[ 1 ].append( 5 )
dfs( 1 , 1 )
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int minimum = int .MaxValue, x, ans;
static List<List< int >> graph = new List<List< int >>();
static List< int > weight = new List< int >();
static void dfs( int node, int parent)
{
if (minimum > Math.Abs(weight[node] - x))
{
minimum = Math.Abs(weight[node] - x);
ans = node;
}
for ( int i = 0; i < graph[node].Count; i++)
{
if (graph[node][i] == parent)
continue ;
dfs(graph[node][i], node);
}
}
public static void Main(String []args)
{
x = 15;
weight.Add(0);
weight.Add(5);
weight.Add(10);;
weight.Add(11);;
weight.Add(8);
weight.Add(6);
for ( int i = 0; i < 100; i++)
graph.Add( new List< int >());
graph[1].Add(2);
graph[2].Add(3);
graph[2].Add(4);
graph[1].Add(5);
dfs(1, 1);
Console.WriteLine( ans);
}
}
|
Javascript
<script>
let minimum = Number.MAX_VALUE, x, ans;
let graph = new Array(100);
let weight = new Array(100);
for (let i=0;i<100;i++)
{
graph[i]=[];
weight[i]=0;
}
function dfs(node,parent)
{
if (minimum > Math.abs(weight[node] - x))
{
minimum = Math.abs(weight[node] - x);
ans = node;
}
for (let to=0;to<graph[node].length;to++)
{
if (graph[node][to] == parent)
continue
dfs(graph[node][to], node);
}
}
x = 15;
weight[1] = 5;
weight[2] = 10;
weight[3] = 11;
weight[4] = 8;
weight[5] = 6;
graph[1].push(2);
graph[2].push(3);
graph[2].push(4);
graph[1].push(5);
dfs(1, 1);
document.write( ans);
</script>
|
Complexity Analysis:
- Time Complexity : O(N).
In dfs, every node of the tree is processed once and hence the complexity due to the dfs is O(N) if there are total N nodes in the tree. Therefore, the time complexity is O(N). - Auxiliary Space : O(1).
Any extra space is not required, so the space complexity is constant.