parent
eded252643
commit
f450678b57
|
|
@ -173,7 +173,27 @@ public:
|
||||||
|
|
||||||
|
|
||||||
Java:
|
Java:
|
||||||
|
```Java
|
||||||
|
class Solution {
|
||||||
|
int sum;
|
||||||
|
public TreeNode convertBST(TreeNode root) {
|
||||||
|
sum = 0;
|
||||||
|
convertBST1(root);
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按右中左顺序遍历,累加即可
|
||||||
|
public void convertBST1(TreeNode root) {
|
||||||
|
if (root == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
convertBST1(root.right);
|
||||||
|
sum += root.val;
|
||||||
|
root.val = sum;
|
||||||
|
convertBST1(root.left);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Python:
|
Python:
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue