Tree/ Graph Interesting Problem
110
Jan 10, 2021
Jan 10, 2021

Given a binary tree/graph where each node have binary data 1 or 0.

Data for the nodes will be given in array where index represents node number and value represents data in the node.

Graph data is corrupter hence our problem to fix that corrupted data and also we are given corrected data in array.

Our task is to find the min steps required to fix the corrupted data.

Each step, we can do

  1. invert one node ie we can convert data from 1 to 0 or 0 to 1
  2. invert complete node & its subtree including that node.

Our task is to find min step required to fix.

public int minSteps(List adjList[], int n, int data[], int correctedData[]){
// write code here
}

Sample input
n = 5
Edges - (u, v)
1 2
2 3
2 4
1 5

data :[0 , 1, 0,1 ,0]
corrected data : [0, 0, 1 , 0, 0]

image

Answer = 1 step
inverting subtree of node 2

Comments (0)