package leetcode;

/**
 * https://leetcode.com/problems/balance-a-binary-search-tree/
 */
public class Problem1382 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public TreeNode balanceBST(TreeNode root) {
        // TODO
        return null;
    }

    public static void main(String[] args) {
        Problem1382 prob = new Problem1382();
    }
}

@@ -12,16 +12,97 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0A%0A
 /**%0A * h
@@ -415,35 +415,837 @@
     
-// TODO%0A        return null
+List%3CTreeNode%3E nodes = new ArrayList%3C%3E();%0A        collectNodes(root, nodes);%0A        LinkedList%3CTreeNode%3E list = new LinkedList%3C%3E();%0A        int i = 0;%0A        list.add(nodes.get(i++));%0A        while (i %3C nodes.size()) %7B%0A            TreeNode current = list.getFirst();%0A            if (i %3C nodes.size()) %7B%0A                current.left = nodes.get(i++);%0A                list.add(current.left);%0A            %7D%0A            if (i %3C nodes.size()) %7B%0A                current.right = nodes.get(i++);%0A                list.add(current.right);%0A            %7D%0A        %7D%0A        return nodes.get(0);%0A    %7D%0A%0A    private static void collectNodes(TreeNode root, List%3CTreeNode%3E nodes) %7B%0A        if (root == null) %7B%0A            return;%0A        %7D%0A        nodes.add(root);%0A        collectNodes(root.left, nodes);%0A        collectNodes(root.right, nodes)
 ;%0A  

Update problem 1382