Leetcode 538:Convert BST to Greater Tree

题目描述:https://leetcode.com/problems/convert-bst-to-greater-tree/#/description


解题思路:后续遍历,每个节点的值等于其自身值加上其已经遍历过的值之和


AC代码

public class Solution {
    int count = 0;
    public TreeNode convertBST(TreeNode root) {
        if(root == null) return null;
        convert(root);
        return root;
    }
    
    public void convert(TreeNode root){
        if(root == null) return;
        convert(root.right);
        count += root.val;
        root.val = count;
        convert(root.left);
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章