菜狗日记2020.11.03
没运行直接提交竟然过了
我的题解几十行...
import java.util.*;
class Solution {
public TreeNode convertBST(TreeNode root) {
ArrayList
traverse(root,list);
int sum=0;
for(int i=list.size()-1;i>=0;i--){
int cur=list.get(i).val;
sum += cur;
list.get(i).val=sum;
}
return root;
}
//中序遍历
private void traverse(TreeNode root,ArrayList
if(root==null){
return;
}
traverse(root.left,list);
list.add(root);
traverse(root.right,list);
}
}
官方题解:
import java.util.*;
class Solution {
//当前遍历的节点的新值
private int sum=0;
public TreeNode convertBST(TreeNode root) {
if(root==null){
return null;
}
convertBST(root.right);
sum += root.val;
root.val=sum;
convertBST(root.left);
return root;
}
}
有被打击到QAQ