public class MyClass {
    
    class NodeTree {
	float val;
	
	NodeTree izq;
	NodeTree der;

	

	public NodeTree(float val) {
		this.val = val;
		
		this.izq = null;
		this.der = null;
	}
	
    }
	
	private NodeTree root;
	
	
    public  int MPF(float[] p, int i, int j)
    {   
        if (i<= j)
        {
        float MP;
        int k = 0;
        float sum_total = 0;
        for (int _i = i; _i <= j; _i ++)
            sum_total += p[_i];
        
        MP = sum_total;
        
        float sum_parcial_left = 0; 
        
        float sum_parcial_right = 0;
        
        for (int _i = i; _i <= j; _i ++)
            {   
                sum_parcial_right = sum_total - (sum_parcial_left + p[_i]);
                float mod = sum_parcial_left - sum_parcial_right;
                
                if(mod <0 )
                mod = -mod;
                
                if(mod < MP)
                {
                    MP = mod;
                    k = _i;
                }
                
                 
                sum_parcial_left += p[_i];
                //if(sumparcial > sum_total/2):
                
            }
            
        return k;
        }
        else return -1;
        
    }
    
    
    public  void printTree( NodeTree node)
    {
        if (node !=null)
        {
        	System.out.println(node.val);
        	printTree(node.izq);
        	printTree(node.der);
        }   
    }
    
    public  NodeTree BuildTree(float[] p, int i, int j)
    {  
        if (i<= j)
        {
        int k = MPF(p, i, j);
        
        NodeTree r = new NodeTree(p[k]);
        r.izq = BuildTree(p, i, k-1);
        r.der = BuildTree(p, k+1, j);
        
        root = r;
        return r;
            
        }
        
        else return null;
    }
    
    
    
    
    
  public static void main(String args[]) {
        float [] p = new float[]{12, 5, 15, 7, 2, 22, 13, 24};
        MyClass t = new MyClass();
        int k = t.MPF(p, 0, p.length-1);
        System.out.println(k);
        
        
        
        NodeTree r = t.BuildTree(p, 0, p.length-1);
        
        
        t.printTree(r);
        
        
     
        
    }
}



Language Version:  JDK 9.0.1
