@shachopin/

# Convert BST to Greater Tree

## No description

Files
• Main.java
• jdt.ls-java-project
Main.java
```1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
```
```class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}

MySolution on lintcode 661. Convert BST to Greater Tree
/**
* Definition of TreeNode:
* public class TreeNode {
*     public int val;
*     public TreeNode left, right;
*     public TreeNode(int val) {
*         this.val = val;
*         this.left = this.right = null;
*     }
* }
*/

public class Solution {
/**
* @param root: the root of binary tree
* @return: the new root
*/

int sum = 0;
public TreeNode convertBST(TreeNode root) {
dfs(root);
return root;

}

private void dfs(TreeNode cur) {
if (cur == null) {
return;
}
//依赖右中左的顺序
dfs(cur.right);
sum += cur.val;
cur.val = sum;
dfs(cur.left);
}
}

=============================================
official solution:

// version: 高频题班
public class Solution {
/**
* @param root the root of binary tree
* @return the new root
*/
int sum = 0;

void dfs(TreeNode cur) {
if (cur == null) {
return;
}
dfs(cur.right);
sum += cur.val;
cur.val = sum;
dfs(cur.left);
}

public TreeNode convertBST(TreeNode root) {