package leetcode;

/**
 * https://leetcode.com/problems/convert-bst-to-greater-tree/
 */
public class Problem538 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

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

    public TreeNode convertBST(TreeNode root) {
        // TODO
        return root;
    }

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

        TreeNode root = new TreeNode(5);
        root.left = new TreeNode(2);
        root.right = new TreeNode(13);

        System.out.println(prob.convertBST(root));
    }
}

@@ -12,16 +12,116 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -432,16 +432,32 @@
     
-// TODO%0A
+if (root == null) %7B%0A    
     
@@ -481,267 +481,1517 @@
     
-%7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem538 prob = new Problem538();%0A%0A        TreeNode root = new TreeNode(5);%0A        root
+    %7D%0A        List%3CInteger%3E list = new ArrayList%3C%3E();%0A        sortedList(root, list);%0A        int sum = 0;%0A        int%5B%5D sums = new int%5Blist.size()%5D;%0A        for (int i = 0; i %3C list.size(); i++) %7B%0A            sum += list.get(i);%0A            if (i == 0) %7B%0A                sums%5Bi%5D = 0;%0A            %7D else %7B%0A                sums%5Bi%5D = sums%5Bi - 1%5D + list.get(i - 1);%0A            %7D%0A        %7D%0A        Map%3CInteger, Integer%3E map = new HashMap%3C%3E();%0A        for (int i = 0; i %3C list.size(); i++) %7B%0A            if (i == 0) %7B%0A                map.put(list.get(i), sum);%0A            %7D else %7B%0A                map.put(list.get(i), sum - sums%5Bi%5D);%0A            %7D%0A        %7D%0A        return convert(root, map);%0A    %7D%0A%0A    private static TreeNode convert(TreeNode node, Map%3CInteger, Integer%3E map) %7B%0A        TreeNode left = null;%0A        TreeNode right = null;%0A        if (node
 .left =
+=
  n
-ew TreeNode(2);%0A        root.right = new TreeNode(13);%0A%0A        System.out.println(prob.convertBST(root)
+ull && node.right == null) %7B%0A            return new TreeNode(map.get(node.val));%0A        %7D%0A        if (node.left != null) %7B%0A            left = convert(node.left, map);%0A        %7D%0A        if (node.right != null) %7B%0A            right = convert(node.right, map);%0A        %7D%0A        TreeNode parent = new TreeNode(map.get(node.val));%0A        parent.left = left;%0A        parent.right = right;%0A        return parent;%0A    %7D%0A%0A    private static void sortedList(TreeNode node, List%3CInteger%3E list) %7B%0A        if (node == null) %7B%0A            return;%0A        %7D%0A        sortedList(node.left, list);%0A        list.add(node.val);%0A        sortedList(node.right, list
 );%0A 

Solve problem 538