Skip to content

Latest commit

 

History

History
44 lines (37 loc) · 997 Bytes

BST_greater_sum_tree.md

File metadata and controls

44 lines (37 loc) · 997 Bytes

1038. Binary Search Tree to Greater Sum Tree

  • relate it to the concepts of constructing a BST from inorder and preorder.
  • you might get the concept of declaring the prefix global.
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int prefix;
    public void dfs(TreeNode root) {
        if (root == null) {
            return;
        }

        dfs(root.right);

        prefix += root.val;
        root.val = prefix;

        dfs(root.left);
    }

    public TreeNode bstToGst(TreeNode root) {
        this.prefix = 0;
        dfs(root);
        return root;
    }
}