Replace greatest sum

What is the error in the code that I have written?
import java.util.*;
public class replace_with_the_greatest_sum {
public class Node{
int data;
Node left;
Node right;
}
static Node root;
public static void getInorderArray(int[]arr){
root=construct(arr, 0, arr.length);
//Node nn=root.left;
root=maxsum(root);
while(root.left!=null){
root.left=maxsum(root.left);
root.left=root.left.left;
}
while(root.right!=null){
root.right=maxsum(root.right);
root.right=root.right.right;
}
int []arr1=new int[arr.length];
preorder(root, arr1, 0);
}
private static Node construct(int[]arr, int lo, int hi){
if(lo>hi){
return null;
}
int mid=(lo+hi)/2;
root.data=arr[mid];
root.left=construct(arr, lo, mid-1);
root.right=construct(arr, mid+1, hi);
return root;
}
public static Node maxsum(Node root){
if(root==null){
return null;
}
root.data=root.data+maxsum(root.right).data;
return root;
}
public static void preorder(Node node, int []arr, int i){
if(node==null){
return;
}
System.out.print(node.data+" ");
preorder(node.left, arr, i);
preorder(node.right, arr, i);
return;

}

public static void main(String[] args) {
	Scanner sc=new Scanner(System.in); 
	int n=sc.nextInt(); 
	int []arr=new int[n]; 
	for(int i=0; i<n; i++){
		arr[i]=sc.nextInt(); 
	} 
	getInorderArray(arr); 

}

}

What is the error in this code? https://ide.codingblocks.com/s/15322

Hi @dktrip,
Your code will give a null pointer exception because root is not being construct. There is an error in the construct function with the initialization of root.data.

Your reply is in reply to which code?
15231 or 15232?

https://ide.codingblocks.com/s/153232 OR https://ide.codingblocks.com/s/153231?

This code.

I have changed the Construct Function of the Cod3e. Please Check.

Hi @dktrip,
Sorry for the delay.
When you calling the construct function also pass the root node to traverse the tree. Node nn is not initialized and it will give you errors.

Hi @dktrip,
Or you need to initialize Node nn=new Node(); where you will need a constructor to call the construct function. Also, call the functions in main using an object of the replace_with_the_greatest_sum class.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.