说实话,本题没有做出来真的是不应该,可能是太困了
    因为是一道Easy题,直接用recusive遍历即可
    而且思路也不难:

    • 就是以 -> -> 的顺序访问即可,顺便用一个累加器来累加

    代码如下:

    1. /**
    2. * Definition for a binary tree node.
    3. * public class TreeNode {
    4. * int val;
    5. * TreeNode left;
    6. * TreeNode right;
    7. * TreeNode(int x) { val = x; }
    8. * }
    9. */
    10. class Solution {
    11. private int accumulator;
    12. public TreeNode convertBST(TreeNode root) {
    13. accumulator = 0;
    14. DFSHelper(root);
    15. return root;
    16. }
    17. private void DFSHelper(TreeNode root) {
    18. if (root == null) {
    19. return;
    20. }
    21. DFSHelper(root.right);
    22. accumulator += root.val;
    23. root.val = accumulator;
    24. DFSHelper(root.left);
    25. }
    26. }