@@ -0,0 +1,1201 @@
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0Apublic class _1382BalanceABinarySearchTree %7B%0A  public TreeNode balanceBST(TreeNode root) %7B%0A    if (root == null) %7B%0A      return null;%0A    %7D%0A    List%3CTreeNode%3E nodes = LDR(root);%0A    root = buildTree(nodes, 0, nodes.size());%0A    return root;%0A  %7D%0A%0A  private List%3CTreeNode%3E LDR(TreeNode node) %7B%0A    List%3CTreeNode%3E nodes = new ArrayList%3C%3E();%0A    if (node.left != null) %7B%0A      nodes.addAll(LDR(node.left));%0A    %7D%0A    nodes.add(node);%0A    if (node.right != null) %7B%0A      nodes.addAll(LDR(node.right));%0A    %7D%0A    node.left = null;%0A    node.right = null;%0A    return nodes;%0A  %7D%0A%0A  private TreeNode buildTree(List%3CTreeNode%3E nodes, int start, int l) %7B%0A    if (l == 1) %7B%0A      return nodes.get(start);%0A    %7D else if (l %3E 1) %7B%0A      int mid = start + l / 2;%0A      TreeNode curRoot = nodes.get(mid);%0A      TreeNode left = buildTree(nodes, start, mid - start);%0A      curRoot.left = left;%0A      TreeNode right = buildTree(nodes, mid + 1, l - mid + start - 1);%0A      curRoot.right = right;%0A      return curRoot;%0A    %7D%0A    return null;%0A  %7D%0A%0A  public class TreeNode %7B%0A      int val;%0A      TreeNode left;%0A      TreeNode right;%0A      TreeNode(int x) %7B val = x; %7D%0A  %7D%0A%7D%0A

Add _1382BalanceABinarySearchTree