- Binary Tree, Tree

Minimum value to be added at each level in Binary Tree to make all level sum equal

import java.util.*;  class Node {    int data;    Node left, right;    public Node(int item)    {        data = item;        left = right = null;    }}  class BinaryTree {    Node root;    public BinaryTree()    {        root = null;    }                  static ArrayList levelOrderAP(Node root)    {                  ArrayList finalAns = new ArrayList();                  if (root == null) {            return finalAns;        }                  Utilfun(root, 0, finalAns);        int maxi = Integer.MIN_VALUE;                  for (int ele : finalAns) {            maxi = Math.max(maxi, ele);        }                        for (int i = 0; i < finalAns.size(); i++) {            int val = maxi - finalAns.get(i);            finalAns.set(i, val);        }        return finalAns;    }              static void Utilfun(Node root,                        int level,                        ArrayList ans)    {        if (root == null) {            return;        }                          if (ans.size() == level) {            ans.add(root.data);        }                          else {            int val = ans.get(level)                      + root.data;            ans.set(level, val);        }                          Utilfun(root.left, level + 1, ans);        Utilfun(root.right, level + 1, ans);    }          public static void main(String args[])    {        BinaryTree tree = new BinaryTree();        tree.root = new Node(8);        tree.root.left = new Node(3);        tree.root.right = new Node(10);        tree.root.left.left = new Node(1);        tree.root.left.right = new Node(6);        tree.root.right.right = new Node(14);        tree.root.left.right.left = new Node(4);        tree.root.left.right.right = new Node(7);        tree.root.right.right.left = new Node(13);          ArrayList ans            = levelOrderAP(tree.root);        System.out.println(ans);    }}